Thanks to visit codestin.com
Credit goes to github.com

Skip to content

matplotlib animations support #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dfroger opened this issue Oct 7, 2016 · 27 comments · Fixed by #687
Closed

matplotlib animations support #150

dfroger opened this issue Oct 7, 2016 · 27 comments · Fixed by #687

Comments

@dfroger
Copy link

dfroger commented Oct 7, 2016

That would be nice to support GIF images, like in this example: http://eli.thegreenplace.net/2016/drawing-animated-gifs-with-matplotlib/

@lesteve
Copy link
Member

lesteve commented Oct 10, 2016

I am not sure how easy it is for us to do. My instinct is why not but it is not that crucial, in general you can select a few static plots that are as clear as an animated gif.

@dfroger
Copy link
Author

dfroger commented Oct 10, 2016

I'll try to implement it for matplotlib animation in general (.git or .mp4). I'll keep you informed.

@lesteve
Copy link
Member

lesteve commented Oct 10, 2016

One of the technical hurdle I can see: after running the example we get the list of figures opened by matplotlib and save each of them (look at save_figures in gen_rst.py for more details). It seems like for matplotlib animations you need to call .save on the matplotlib.Animation object instead.

@Titan-C
Copy link
Member

Titan-C commented Oct 10, 2016

I haven't used animation. But if is a different object, you should be able to write a new function(as we do for mayavi figures) an make it part of the chain.
Other thing that is not a priority I guess, but might be good to keep in mind is our support for latex/pdf export, see #61.

@dfroger
Copy link
Author

dfroger commented Oct 10, 2016

@lesteve Yes, I think I will add an if case like the one for mayavi, in which I will call .save of matplotlib.Animation.

@Titan-C ok, I'll keep this in mind.

I can't work on it right now, but I will spend some time on it this week, so it may progress slowly :)

@NelleV
Copy link
Contributor

NelleV commented Oct 21, 2016

In general, it would be super useful for matplotlib to be able to display animation, so we would greatly appreciate this addition to sphinx-gallery.

@lesteve
Copy link
Member

lesteve commented Oct 22, 2016

@NelleV here is the code we use to find all the matplotlib figures that have been generated by the example and save each of them:

fig_managers = matplotlib._pylab_helpers.Gcf.get_all_fig_managers()

I am not a matplotlib expert at all, so here is a naive question: is there a way from the figure object to know that a matplotlib.Animation object was used to plot on this figure?

@NelleV
Copy link
Contributor

NelleV commented Oct 22, 2016

There is an animation object.
I'll check the code out and see whether we can extend it to movies.

@lesteve lesteve changed the title gif support matplotlib animations support Nov 23, 2016
@t-makaro
Copy link

t-makaro commented Jul 29, 2018

I really need this feature. For the docs that I'm currently working on, every single example is an animation. A file of the name plot_<name>.py will produce a file <name>.gif (from matplotlib.Animation.save). It'd be great if this could include those .gifs as the output plot instead. It's really useless to have a gallery of static animations.

@larsoner
Copy link
Contributor

#313 would make this easier presumably because you could write your own scraper

@choldgraf
Copy link
Contributor

note that #313 has been merged now, it'd be useful if somebody could give a shot at this with gifs!

@t-makaro
Copy link

t-makaro commented Sep 9, 2018

I'm just waiting on a release.

@Titan-C
Copy link
Member

Titan-C commented Sep 10, 2018

@t-makaro is an experimental feature, you are better of giving it a try on the git version. And check what is possible and what still needs to be done.

@t-makaro
Copy link

That's an awful lot of extra work to experiment with something that I won't yet be able to use. My docs are built with readthedocs, so I'm not sure that I could (or even should) get rtd to build with a dependency from git. Also, my gallery is currently built with nbsphinx, so I don't want to end up maintaining 2 galleries until this is released.

I may try to experiment a little with this on the weekend, and I could report back on my success, but even a beta release would be greatly helpful.

@larsoner
Copy link
Contributor

Does it make it easier to experiment knowing you can pip install or add to requirements.txt a git master dependency with:

pip install "https://api.github.com/repos/sphinx-gallery/sphinx-gallery/zipball/master"

?

@t-makaro
Copy link

Yes, yes that might. Thank you.

@choldgraf
Copy link
Contributor

@t-makaro please do share thoughts on how this works for you if you give it a shot!

@t-makaro
Copy link

So, I've been trying to modify the "naïve scrapper" to grab gifs. https://sphinx-gallery.readthedocs.io/en/latest/configuration.html#image-scrapers

The first issues that I've been having are:

from sphinx_gallery.gen_rst import figure_rst

should be

from sphinx_gallery.scrapers import figure_rst

This is also a mismatched parathesis on the example on the docs.

I can't get the example to work in any shape or form. My build fails at:

pickling environment...
Exception occurred:
  File "e:\tmoko\anaconda3\envs\animatplot\lib\site-packages\sphinx\environment\__init__.py", line 148, in dump
    pickle.dump(env, f, pickle.HIGHEST_PROTOCOL)
_pickle.PicklingError: Can't pickle <function GIFScraper at 0x000001DBF786BBF8>: attribute lookup GIFScraper
on __main__ failed

@choldgraf
Copy link
Contributor

Good catch @t-makaro ...it looks like we need to update the documentation there. Maybe @larsoner has a moment to make sure that the suggested code there works? I wonder if creating a scraper is advanced enough that it could use its own section in the Advanced configuration page rather than on the "configuration" page. Then we could go into more step-by-step detail.

@larsoner
Copy link
Contributor

It sounds like @t-makaro has already figure out the necessary fixes to at least remove the errors, can you make a PR?

@t-makaro
Copy link

I have no idea what caused the pickling error (it occurred with both the function and the class). Is there any place that I should put my image scraper? I was just temporarily placing it in my conf.py to try to get something to work, but there must be a proper place. I'm still new to sphinx.

@choldgraf
Copy link
Contributor

@larsoner if you're +1 to the idea then I can make a PR!

@larsoner
Copy link
Contributor

Yeah go ahead

@choldgraf
Copy link
Contributor

@larsoner and @t-makaro see #417 which tries to clarify how to create image scrapers, and also adds the ability to embed images saved to disk within the example (which should solve your GIF use-case, I believe)

@larsoner
Copy link
Contributor

Coming back to this because I have a couple of use cases that could benefit from native sphinx-gallery support for Matplotlib animations. Has anyone written a scraper yet?

I noticed the matplotlib dev docs say about animations that you can save animations then upload them and use a .. raw:: html tag to display them in the built docs, but this seems a bit unsatisfactory. So @choldgraf any idea if there would be a desire for this at the matplotlib end to simplify their processes? Also any insight into the best way to do it?

It seems like using the FuncAnimation.to_html5_video or FuncAnimation.to_jshtml might be the nicest way to do it, followed by just FuncAnimation.save with .gif or similar ending. I guess we could make this an option like:

matplotlib_animations : False (default, backward compat) | True (html5?) | 'html5' | 'jshtml' | tuple

where tuple would be 2 elements, the first the method name to use and the second a dict to pass as kwargs, so you could for example do ('jshtml', dict(fps=30)). But to start I would probably just enable False | True after trying a few options and picking whichever seems to work best for True.

@choldgraf
Copy link
Contributor

Hmmmm - I'm not sure how the MPL devs would feel about it, though in general they've been pretty receptive to improvements like this as long as it gives them the flexibility to keep the customizations they want. I'd be +1 on this (maybe with some warning about YMMV, since I could see this becoming a rabbit-hole)

@larsoner
Copy link
Contributor

since I could see this becoming a rabbit-hole

Indeed, the idea wouldn't be to cover all mpl (and other) use cases, but if we can satisify 80-90% of them with some simple interface like matplotlib_animations: True then I think we should. And then if people want other/fancy options, they have some code to work from to write their own scraper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants