1

I am new to matplotlib and was trying out animation. Watched few tutorials from youtube and created below code, which is pretty much copy/paste from one of the tutorials. I was expecting the plot to be continuously updated with new data points, but it is not happening. I added the print statements for trouble shooting and saw that xcord and ycord were printed just once - meaning the animate function does not seem to be called at all. Please help to figure out what I am doing wrong. I am running this code in Spyder 4.1.5

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
import random

xcord = []
ycord = [5]

index = count()
xcord.append(next(index))

def animate(i):
    xcord.append(next(index))
    ycord.append(random.randint(0,5))
    print(xcord)
    print(ycord)
    plt.cla()
    plt.plot(xcord,ycord) 
 
fig, ax = plt.subplots(1,1)
print(xcord)
print(ycord)
ani = FuncAnimation(fig,animate,interval=1000)
plt.tight_layout()
plt.show()
  • Please refer to the [official reference for a sample of line animation](https://matplotlib.org/stable/gallery/animation/simple_anim.html#sphx-glr-gallery-animation-simple-anim-py). The basic structure of the animation is to initially set up the graph, and then update the data to be drawn in the animation function. – r-beginners Sep 25 '21 at 11:57
  • Ok Thank you. I get your point regarding the initial setup of the graph. But I cut and pasted the example from the link above and it just drew a sine wave (from the line that does the initialization, I think) but no animation is happening. Any thoughts? – Peter John.V. Sep 25 '21 at 15:41

1 Answers1

0

I will answer by modifying the animation from the official one to fit your code. The line color is red and the line width is set to 3. There's no reason to specify the number of frames and turn off repetition. You can customize the rest as you like.

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
import random

xcord = []
ycord = [5]

index = count()
xcord.append(next(index))

fig, ax = plt.subplots(1,1)
ax.set(xlim=(0,20), ylim=(0, 5))
line, = ax.plot([], [], 'r-', lw=3)

def animate(i):
    xcord.append(next(index))
    ycord.append(random.randint(0,5))
    line.set_data(xcord, ycord)

ani = FuncAnimation(fig, animate, frames=19, interval=200, repeat=False)

plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • I got the animation to work. But my problem seems to be in Anaconda/Spyder. Even the code you gave, if I copy/paste on to my Spyder window and try to run, it comes up with a blank figure. So I tried a fresh install of python and pasted the code onto the REPL window and there it was working. If I try any of the other example code blocks I had tried earlier in my Anaconda/Spyder works fine in the REPL window of new install. If there is any config to be done on Anaconda, pls let me know. Otherwise I am good to play around with animation for now. Thanks @r-beginners – Peter John.V. Sep 26 '21 at 07:40
  • You may need to specify a backend as described in [this answer](https://stackoverflow.com/questions/35856079/animation-from-matplotlib-not-working-in-spyder). `%matplotlib qt` ? – r-beginners Sep 26 '21 at 08:34
  • Great. This question is exactly same as mine and the solution mentioned there worked for me in Spyder. Thanks for pointing me to this question from the past – Peter John.V. Sep 26 '21 at 10:16