Improve saving animated GIF with ffmpeg #18093
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
As of #17401, we enabled an error-free path to save animated GIFs with ffmpeg, which is the default writer. This means with matplotlib 3.3.0 the follow code works without error:
Unfortunately, it also produces this by default (notice the weird coloring):
This is a consequence of GIF being a palette-d image format and the default palette used by ffmpeg being...suboptimal for matplotlib (I'm pretty sure anti-aliasing plays a role, otherwise there are only 3 colors in the original image). Luckily this blog from GIPHY, which helped me understand this, also provides some magic incantations for ffmpeg to get it to determine an optimal palette for us. The result is:
Best of all, if I run that same animation through ffmpeg, imagemagick, and pillow we see:
So our default writer now produces our smallest animated GIFs.
I also fixed a bug where parameters to
save()
likecodec
were not being passed and set in the base class--thus the rcParam defaults were always used. (Introduced in #15961.) I tried to add a test for this, but couldn't figure out how since you can't pass them along with a class instance, which was the straightforward path to check they're set properly.I'll leave it to @QuLogic to decide whether this belongs in 3.3.1.
PR Checklist