Skip to content

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

Closed

Description

@story645

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)

Activity

  1. dopplershift commented on Sep 16, 2022

    @dopplershift
    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?

  2. story645 commented on Sep 16, 2022

    @story645
    MemberAuthor

    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.

  3. dopplershift commented on Sep 16, 2022

    @dopplershift
    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.

  4. timhoffm commented on Sep 16, 2022

    @timhoffm
    Member

    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

  5. StefRe commented on Sep 17, 2022

    @StefRe
    Contributor

    something like this?
    animation

  6. timhoffm commented on Sep 17, 2022

    @timhoffm
    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.

  7. story645 commented on Sep 18, 2022

    @story645
    MemberAuthor

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

  8. added this to the v3.6.1 milestone on Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions