Improve saving animated GIF with ffmpeg#18093
Merged
tacaswell merged 2 commits intomatplotlib:masterfrom Jul 29, 2020
Merged
Conversation
The default GIF palette from ffmpeg looks terrible with our plots. With some ffmpeg magic, we can have it autogenerate a palette from the frames of the animation.
Missed forwarding a couple of parameters when `AbstractMovieWriter` gained an `__init__`.
timhoffm
approved these changes
Jul 29, 2020
tacaswell
approved these changes
Jul 29, 2020
meeseeksmachine
pushed a commit
to meeseeksmachine/matplotlib
that referenced
this pull request
Jul 29, 2020
QuLogic
added a commit
that referenced
this pull request
Jul 29, 2020
…093-on-v3.3.x Backport PR #18093 on branch v3.3.x (Improve saving animated GIF with ffmpeg)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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()likecodecwere 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