4

Hello Dear StackOverfloooow Members, I am having trouble understanding the FuncAnimation module of matplotlib. Would you mind helping me out a bit? I have two questions:

  1. Why does both die init and animate function need a comma after giving back only PLOT?
  2. Why does my code not update the time_text? If I let it print t after every animation i corectly adds one up in console, but the text does not get updated in the plot.

.

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

fig = plt.figure()
sub = fig.add_subplot(111,xlim=(0, 10), ylim=(0, 1))
PLOT, = sub.plot([],[])
time_text = sub.text(1,1,"",transform = sub.transAxes, ha="right")
t = 0

def init():
    PLOT.set_data([],[])
    time_text.set_text("")
    return PLOT,time_text

def animate(i):
    global t
    x = np.linspace(0,10,1000)
    y = np.exp(- ((x-0.01*i)/(2))**2 )/np.sqrt(2*np.pi)
    t += 1

    PLOT.set_data(x,y)
    time_text.set_text("time = "+str(t))
    return PLOT, time_text

ani = animation.FuncAnimation(fig, animate, init_func=init, frames=2000, interval=20, blit=True)

plt.show()

3 Answers 3

4

1) Not really sure what you mean. In both init and animate you need to return both PLOT and time_text, so you seperate them with a comma so each function will return a tuple.

For the 'hanging' comma in PLOT, = sub.plot([],[]), sub.plot is returning a list with a single element, a matplotlib.lines.Line2D object. The PLOT, is unpacking this single element list. So, you can also do this:

PLOT = sub.plot([],[])[0]

To get the element.

2) Your code does update time_text, you are just drawing it out of the bounds of the graph. For example change:

time_text = sub.text(1,1,"",transform = sub.transAxes, ha="right")

To:

time_text = sub.text(1,0,"",transform = sub.transAxes, ha="right")

To get it to display in the bottom right, or 0.5, 0.5 to get it to display in the middle of the screen.

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

2 Comments

Okay, I understand, thank you very much. About the comma thing: For example, if I completly omit the time display and only return the new plotdata: return PLOT i get an error. But if I return return PLOT, it works. why is that?
PLOT, unpacks what's in PLOT. Try this at the console: a, = [1]. Then print a.
1

The other answers do not entirely answer the first question.

The return statement of init and animate is needed strictly only when blit=True is used. Those functions are expected to return an iterable of artists to update for the animation. Having two artists, the return could look like

return artist1, artist2
# or 
return [artist1, artist2]

If a single artist shall be updated, one has to remember that still an iterable is needed. Using the comma after that single artist is then just the easiest way to make the function return an iterable

return artist1,
# or 
return [artist1]

Comments

0

In python tuples are written using parentheses and commas, e.g. (3,2) which is a 2-tuple. A one tuple would be (2,) which has length 1. This is distinguished from the int 2 which does not have length defined at all. Note: () is the 0-tuple with empty content.

In this case the function returns an enumerable of length one, so omitting the comma would make PLOT an enumerable. With the comma sign the contents of the tuple PLOT, are matched to the contents of the returned value.

Parentheses can be omitted in assignments, so PLOT, is the same as (PLOT,).

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.