How to create indefinite funcanimation frames dyna

2019-06-25 12:40发布

问题:

I am trying to recreate a population variance graph in python

In that example, as soon as we start, the function runs immediately to I guess the limit of the environment set by website.

I have managed to create similar graphs, but for animation I am stuck. Below is my code.

import matplotlib.animation as animation

fig, ax = plt.subplots(1,1,figsize=(5,4))
plt.close()

frameRate = 30

global_counter = 0

def animate(i):

    ax.clear()

    global global_counter

    ax.text(0.5,0.5, 'test:{}'.format(global_counter))
    global_counter += 1


ani = animation.FuncAnimation(fig, animate, np.arange(1,1000), interval=frameRate)

plt.tight_layout()
from IPython.display import HTML
HTML(ani.to_html5_video())

Output:

The problem is, the execution time is directly proportional to the number of times and then graph is generated. So if 1000 as above or more, it takes considerable time before generating the graph. Looks like it generates all 1000 frames before outputting the graph. I would need about at least 20000 frames this way. Instead, it should be live, and update as long as website is opened or to an upper limit set without compile time compromise.

And next problem is, after 1000, the counter starts all over again. Shouldn't the global counter continue to increase?

I want

  1. Program to start immediately and then update dynamically like in example I quoted.
  2. Program to continue increasing counter or not resetting variables after frames count is over. If not matplotlib, seaborn or plotly or any other lib could help here?

I am using Python 3.x in ipython notebook (anaconda environment).

回答1:

ani.to_html5_video() creates a file. For this file to be created, all frames need to be known in advance. Hence the animation is run once in completeness, then those frames are saved and converted to html5 video.

If you want to see the animation live, you may use the %matplotlib notebook backend without saving the animation.

As for the 1000 frames, you are setting that number yourself in the third argument to FuncAnimation, np.arange(1,1000). Either remove that argument or chose a different number here, e.g. frames =20000.