Skip to content

Animation Tutorial #24274

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

Merged
merged 29 commits into from
Nov 23, 2022
Merged

Animation Tutorial #24274

merged 29 commits into from
Nov 23, 2022

Conversation

chahak13
Copy link
Contributor

PR Summary

A one-stop tutorial that covers all the major points of creating an animation using the matplotlib.animation module. (Reference issue: #24228 by @story645)

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@story645 story645 self-assigned this Oct 25, 2022
@story645
Copy link
Member

What sort of feedback would be helpful at this stage?

@story645 story645 linked an issue Oct 25, 2022 that may be closed by this pull request
@chahak13
Copy link
Contributor Author

I don't think I've added that much right now to have any suggestions particularly, but just in general, is the type of animation okay for the plots? I was thinking to keep them very simple and distraction-free so as to focus more on the process of animating rather than the data that it animates.

@story645
Copy link
Member

story645 commented Oct 25, 2022

Yeah if anything I'd suggest going even simpler and just using sin/cos/unit circle so you can remove the data generation functions

@chahak13
Copy link
Contributor Author

@story645 can you take a look at this? I think I have this as a first draft and would appreciate feedback! I had a question too, actually. I'm not sure why the Lines2D animation is not showing up correctly. I tried saving it locally as .html and that seems to be working fine but for some reason, it isn't working correctly in the docs.

@chahak13
Copy link
Contributor Author

@story645 thanks for the feedback! I've updated most of the things. Let me know if there are more things to change :)

@chahak13
Copy link
Contributor Author

Something's still wrong with the line plot though. Working on it.

The animation for Lines2D was not showing the animation properly but was
showing just the final frame. Adding an init function resolved this
issue and the animation appears correctly now.
@chahak13
Copy link
Contributor Author

Okay, the line plot seems to be working now. For some reason, adding an init_func resolved the issue where it was showing just the final frame. Maybe it is because if there's no function, it just starts out with the final frame? I don't know, just speculating. I'm not sure if this would be the ideal solution for it either. Let me know if there's a better way to resolve this. Thanks!

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the bones are awesome! I also think this is in large enough scope that it needs a second review.

I left it out of my outline so my mistake, but I think it'd be useful to briefly go through the various methods on animation with possibly putting to_jshtml and to_html_video at the bottom as part of a saving section

edit: also my bias for the line is something simple unless you can explain what the deleting values is about. Alternatively, I was able to remove the init by pulling the axes setting outside the init ax.set(ylim=(-1.1, 1.1), xlim=(0, 5))

Comment on lines 99 to 105
fig, ax = plt.subplots()
rng = np.random.default_rng()
t = np.linspace(-4, 4, 400)
a, b = 3, 2
delta = np.pi / 2

scat = ax.scatter(np.sin(a * t[0] + delta), np.sin(b * t[0]), c="b", s=2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like reusing an example and showing a different method of doing the same thing, so I'd suggest then you reuse scatter. Or pick something that's really well suited to artist animation, but I'm honestly not sure what that would be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Animating Markers" and "Animating Images" are structurally very similar to "Animating Lines". IMHO they don't explain something new (other than you use different set_* methods to manipulate the artists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the different set_* methods is kind of the point, unless there's a different way to structure this so it's clear that the key to animation is finding that artists set_data equivalent -> maybe a table?

@chahak13
Copy link
Contributor Author

chahak13 commented Nov 2, 2022

Alternatively, I was able to remove the init by pulling the axes setting outside the init ax.set(ylim=(-1.1, 1.1), xlim=(0, 5))

I had the same thing before but what this led to was that the animation used to get stuck at the last frame after building the docs. This did not happen if I just saved the animation as .html
image

@chahak13
Copy link
Contributor Author

chahak13 commented Nov 2, 2022

I changed it to the simple example you linked and that works for some reason, so let me put that instead!

@story645
Copy link
Member

story645 commented Nov 2, 2022

Forgot that the decay example doesn't have explicit frames but the docs save out to 100 and that might be why things are inconsistent.

@chahak13
Copy link
Contributor Author

chahak13 commented Nov 2, 2022

Ah okay. Yeah, that might be the case. Thanks for that!

@chahak13
Copy link
Contributor Author

chahak13 commented Nov 3, 2022

@story645 I changed to an example that shows a 2d gaussian pdf with increasing covariance (kind of a raindrop effect). I feel that this shows the changes in the image in a better way and also allows the example to not be extremely convoluted. This also allows reusing the same in ArtistAnimation. Let me know, thanks!

@story645
Copy link
Member

story645 commented Nov 3, 2022

Wondering if for the artist animation you should add an additional artist to each list? Maybe a marker that moves across the page or something?

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pinging me, I hope these are minor changes.

I think this is a great starting base that has all the important bits.

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It exists! I think this has all the important bits written out in a way that's more user friendly than the API docs and isn't overwhelming 😁

I'm not sure if this is significant enough to need a 2nd review.

@chahak13 chahak13 changed the title [WIP] Animation Tutorial Animation Tutorial Nov 17, 2022
@story645
Copy link
Member

Hey folks, since this is documentation my plan is that if there are no reviews over the weekend then I'll merge Monday if there are no objections.

@chahak13
Copy link
Contributor Author

Thanks @QuLogic! That should fix all of the comments.

@story645 story645 requested a review from QuLogic November 21, 2022 19:42
@QuLogic QuLogic merged commit de58d98 into matplotlib:main Nov 23, 2022
@QuLogic
Copy link
Member

QuLogic commented Nov 23, 2022

I squash merged as it's unlikely we need all 29 commits preserved.

melissawm pushed a commit to melissawm/matplotlib that referenced this pull request Dec 19, 2022
* First version of animation tutorial

* Simplify examples by removing data gen functions

* Add tutorial text

* Format to agree with flake8

* Update tutorial and scatter example

* Fix example for line animation

The animation for Lines2D was not showing the animation properly but was
showing just the final frame. Adding an init function resolved this
issue and the animation appears correctly now.

* Change to simpler line animation example

* Change headings and update set_offsets description

* Change writer type to pipe-based from standard

* Update saving animation section

* Add new example for images and ArtistAnimation

* Remove unused variables

* Correct type of animation class

* Remove unused variable

* Correct wrong correction

* Update ArtistAnimation example to use bar chart

* Barchart race example for ArtistAnimation

* Change FuncAnimation to have one general guideline

* Add a table for set methods for example artists

* Add plotting method, return artist type table

* Reword to avoid confusion

* Reposition note about pipe based writers

* Change color and don't sort bar chart

* Remove animation code in save animation section

* Change inline codeblocks to clear CI

* Make small corrections as per code review

* Remove :class: from links

* Set the animation range so that it loops

* Update to better animation range for scatter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Doc]: Create Animation Tutorial
6 participants