user62089
user62089

Reputation: 561

FuncAnimation Matplotlib difficulty

I am having some difficulty trying to get FuncAnimation working in Matplotlib. Here is what I am trying to achieve. I have linexdata and lineydata

linexdata = [[0, 0.96663913603170604, 1.8304227404767459], [0, 0.96734736607357541, 1.8288493988572816], [0, 0.96802166208581697, 1.8272797290389486], [0, 0.96866363556090329, 1.8257115495669918]]
lineydata = [[0, 0.25614211034477896, 0.76000507255964045], [0, 0.253454282564955, 0.76120840368022669], [0, 0.25086662139992305, 0.76240896996548169], [0, 0.24837624915022258, 0.76361296474589158]]

The actual lengths of the 2 lists is around 2000. I am just showing a few data points to illustrate what I am trying to do.

I want to show an animation for the linedata.

Here's is my code:

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

fig = plt.figure(figsize=(7,7));
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(-0.5,3), ylim=(-0.5,3))
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
line.set_data([], []);

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(linexdata[i] ,lineydata[i]);
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20)

plt.show()

The output plot is empty. I somehow can't get this working mainly because I don't understand how to work with FuncAnimation and I am not able to figure out where the error is. Any help is appreciated.

Thanks.

Upvotes: 1

Views: 971

Answers (1)

snake_charmer
snake_charmer

Reputation: 3013

What happens here is that FuncAnimation recursively calls on your animate function. Each times it increments the variable i, that you use to index your data, with 1. The number of recursions is controlled by the argument "frames" when you call the function.

Since your linexdata list only contains 4 lists, and considering that you called Funcanimation with frames=200, python crashes with IndexError after the first 4 calls. Adapting the number of frames to the size of your data is the solution to your problem.

The reason why you cannot see anything (i.e. not even the 4 first lines) is because it goes to quick for us to see anything. interval=20 means that it goes 20 milliseconds between two calls to animate. Increasing this number to, for example, 500 will let you see what happens. (For the sake of clarity, I took the freedom to modify you lists slightly so that changes between 2 frames appears clearly).

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

linexdata = [[0, 0.96663913603170604, 1.8304227404767459], [0, 0.96734736607357541, 2.8288493988572816], [0, 0.96802166208581697, 3.8272797290389486], [0, 0.96866363556090329, 4.8257115495669918]]
lineydata = [[0, 0.25614211034477896, 0.76000507255964045], [0, 0.253454282564955, 0.76120840368022669], [0, 0.25086662139992305, 0.76240896996548169], [0, 0.24837624915022258, 0.76361296474589158]]


fig = plt.figure(figsize=(7,7));
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(-0.5,3), ylim=(-0.5,3))
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
line.set_data([], []);

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(linexdata[i] ,lineydata[i]);
    return line,


anim = animation.FuncAnimation(fig, animate, init_func=init, frames=4, interval=500)

plt.show()

Upvotes: 1

Related Questions