Skip to content Skip to sidebar Skip to footer

Matplotlib Animation Multiple Datasets

I have multiple datasets that I want to plot at the same time in a matplotlib animation. Is this possible? Each dataset is an array of (x,y) co-ordinates, so I want to be to animat

Solution 1:

the animation function is only called once per frame, so you have to make sure you're updating both of your plots in that one single call.

For example:

def update_lines(num, data1, data2, line1, line2):
    line1.set_data(data1[...,:num])
    line2.set_data(data2[...,:num])
    return line1,line2

data1 = np.random.rand(2, 25)
data2 = np.random.rand(2, 25)

fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax2 = fig1.add_subplot(122)
l1, = ax1.plot([], [], 'r-')
l2, = ax2.plot([], [], 'g-')
for ax in (ax1, ax2):
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_xlabel('x')
    ax.set_title('test')
line_ani = animation.FuncAnimation(fig1, update_lines, 25, fargs=(data1, data2, l1, l2),interval=50, blit=False)

Post a Comment for "Matplotlib Animation Multiple Datasets"