How to speed up a Matplotlib animation?

215 Views Asked by At

I have about 8 months worth of hourly data that I want to plot in an animation style. I am currently able to do that, however it gets extremely slow as the quantity of data increases. Note that I have even set the interval to only 1ms! Is there anyway to ensure that the animation doesn't slow down? Furthermore, how can I plot multiple lines at the same time in this style?

Here is my code so far:

x = benchmark_returns.index
y = benchmark_returns['Crypto 30'] 
#Would preferrably like to plot 
#benchmark_returns[['Crypto 30', 'NASDAQ', 'Dow Jones 30', 'S&P 500']] at the same time

fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')

def update(num, x, y, line):
    line.set_data(x[:num], y[:num])
    return line,


ani = animation.FuncAnimation(fig, update,  fargs=[x, y, line],
                              interval = 1, blit=True)
plt.show()

Here is an example of my dataframe:

                     Crypto 30  Dow Jones 30  NASDAQ  S&P 500
2019-06-09 00:00:00  100.00000         100.0   100.0    100.0
2019-06-09 01:00:00   95.78653         100.0   100.0    100.0
2019-06-09 02:00:00   95.78653         100.0   100.0    100.0
2019-06-09 03:00:00   95.78653         100.0   100.0    100.0
2019-06-09 04:00:00   95.78653         100.0   100.0    100.0
2019-06-09 05:00:00   95.78653         100.0   100.0    100.0
2019-06-09 06:00:00   95.78653         100.0   100.0    100.0
2019-06-09 07:00:00   95.78653         100.0   100.0    100.0
2019-06-09 08:00:00   95.78653         100.0   100.0    100.0
2019-06-09 09:00:00   95.78653         100.0   100.0    100.0
1

There are 1 best solutions below

0
William Miller On BEST ANSWER

Displaying an animation with plt.show() is limited by the speed with which the computer can redraw the plot - consequently the interval will not necessarily dictate the actual rate at which the animation will update. Saving the animation via

ani.save("animation.mp4")

or similar will allow you to view the animation at the specified speed.