1

I need to animate a figure with subplot. For that, I use this official example :

http://matplotlib.org/examples/animation/subplots.html

Now, I would like have access to this feature through a function. So, I replace the 3 last lines of this official example

ani = SubplotAnimation()
# ani.save('test_sub.mp4')
plt.show()

by :

def MyDraw():            
    ani = SubplotAnimation()
    plt.show()

MyDraw()

The official example works perfectly but calling this through the function MyDraw does not work. It produces no error message, but the figure show each axes with no curve inside...

If someone could help me. Thanks.

2
  • Could you please explain what means through another function? Can you provide its code? Commented Feb 16, 2017 at 14:24
  • I speak about the function MyDraw() as suggets in my post. I edit the post to clarify this point. Commented Feb 16, 2017 at 14:29

2 Answers 2

1

You must keep a reference to the animation object or it (along with it's timers which update the plot) get garbage collected.

See http://matplotlib.org/api/animation_api.html#animation

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks a lot tacaswell for your help.

So, the solution is to keep a reference to the animation object like that:

def MyDraw():            
    ani = SubplotAnimation()
    plt.show()
    return ani

ani=MyDraw()

even if you have no need to deal with the variable ani thereafter.

Note also that

ani = SubplotAnimation()
plt.show() 

works fine, but

SubplotAnimation()
plt.show()

does not work, for exactly the same reason.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.