How to reset matplotlib animation without restarting script

I use the matplotlib FuncAnimation function to animate a segment of a large data set:

fig = plt.figure(figsize=(15, 11.5))
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
                     xlim=(x1,x2), ylim=(y1, y2))

data,=ax.plot([],[],'o')

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


def animate(t):
    global x_pos,y_pos
    data.set_data(x_pos[t],y_pos[t])
    return data

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


plt.show()

When I run my code from scratch, this works fine. However, since this involves loading and pre-processing a large data set, it takes several minutes, so I would like to be able to run only part of my code for testing and animation.

When I close my animation and start it again, I only have an empty digit - no dots are drawn, and the animate () function is never called (I tested this with the print statement).

I tried to clear the graph and figure:

plt.clf()
fig.close()
plt.clear(figure)

and try a different digit number, but the results are the same.

, script?

+4
1

:

ani.frame_seq = ani.new_frame_seq() 
+1

Source: https://habr.com/ru/post/1649223/


All Articles