Skip to content

[Doc]: add animation examples to show animated subplots #23909

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
story645 opened this issue Sep 16, 2022 · 7 comments · Fixed by #23929
Closed

[Doc]: add animation examples to show animated subplots #23909

story645 opened this issue Sep 16, 2022 · 7 comments · Fixed by #23929

Comments

@story645
Copy link
Member

story645 commented Sep 16, 2022

Documentation Link

https://matplotlib.org/devdocs/gallery/animation/index.html

Problem

Figured it'd be useful to have at least one example in the docs to show that it's possible and encourage the use of the OO paradigm. There used to be a subplot example, but that was back when it was overly complicated to do animated subplots. it was removed in #10125

Suggested improvement

Add a new example showing how animation on subplots work. I originally thought of merging existing examples but they all feel overly complicated so I think this should be super simple, like 3 plots: sine/cosine/tangent and two lines each in the style of https://matplotlib.org/devdocs/gallery/animation/simple_anim.html#sphx-glr-gallery-animation-simple-anim-py

I was thinking something like this, but if there's an easier or alternative, we should probably show that (too/instead of):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, (ax1, ax2, ax3) = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line1, = ax1.plot(x, np.sin(x))
line2, = ax2.plot(x, np.cos(x))
line3, = ax3.plot(x, np.tan(x))

def animate(i):
    line1.set_ydata(np.sin(x + i / 50))  # update the data.
    line2.set_ydata(np.sin(x + i / 50)) 
    line3.set_ydata(np.sin(x + i / 50)) 
    return [line1, line2, line3]


ani = animation.FuncAnimation(
    fig, animate, interval=20, blit=True, save_count=50)
@dopplershift
Copy link
Contributor

but that was back when it was overly complicated to do animated subplots.

I'll agree that the example was overly complicated, but I don't think anything has changed in regards to animating. They are, and have always been, just another Axes. Is there an issue I missed?

@story645
Copy link
Member Author

story645 commented Sep 16, 2022

Is there an issue I missed?

That example was removed in #10125 and also yes I saw code and didn't read carefully that actual mechanisms didn't change 😖 sorry!

so currently the only examples in the gallery are the single axes case. I think it's one of those things that's obvious if you've got a good intuition for the library and not so much if you don't. I got a DM asking about how to animate subplots - sent them to the usage guide and discourse - and it seemed like a useful thing to include to show the intuition.

@dopplershift
Copy link
Contributor

That example was removed in #10125 and also yes I saw code and didn't read carefully that actual mechanisms didn't change 😖 sorry!

No worries, I think having an example that shows how to properly manage artists across different Axes is definitely useful to have. Just wanted to make sure I wasn't misunderstanding something else.

@timhoffm
Copy link
Member

timhoffm commented Sep 16, 2022

I think a projection of a circular motion would be a nice example. Similar to https://www.youtube.com/watch?v=cx-qHZwsLqs

Left ax: circle with a rotating point and a line from that point vertically to the x axis(= the y component)

Right ax: y(r) = sin(r), and a vertical line moving along r. One should see that both vertical lines always have the same height.

A277AC4B-DCC4-4D05-BA6D-0C24DA533345

@StefRe
Copy link
Contributor

StefRe commented Sep 17, 2022

something like this?
animation

@timhoffm
Copy link
Member

Great! I wasn't sure how difficult the connection line between the two Axes was, and thus proposed the vertical lines, but this is even bette.

@story645
Copy link
Member Author

@timhoffm & @StefRe that is a far better suggestion than mine! PR would be much appreciated!

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

Successfully merging a pull request may close this issue.

5 participants