Matplotlib animation too slow ( ~3 fps )


I need to animate data as they come with a 2D histogram2d ( maybe later 3D but as I hear mayavi is better for that ).

Here's the code:

import numpy as np
import numpy.random
import matplotlib.pyplot as plt
import time, matplotlib


# Generate some test data
x = np.random.randn(50)
y = np.random.randn(50)

heatmap, xedges, yedges = np.histogram2d(x, y, bins=5)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

# start counting for FPS
tstart = time.time()

for i in range(10):

    x = np.random.randn(50)
    y = np.random.randn(50)

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=5)

    plt.imshow(heatmap, extent=extent)

# calculate and print FPS
print 'FPS:' , 20/(time.time()-tstart)

It returns 3 fps, too slow apparently. Is it the use of the numpy.random in each iteration? Should I use blit? If so how?

The docs have some nice examples but for me I need to understand what everything does.

2/14/2017 8:36:13 AM

Accepted Answer

Thanks to @Chris I took a look at the examples again and also found this incredibly helpful post in here.

As @bmu states in he's answer (see post) using animation.FuncAnimation was the way for me.

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

def generate_data():
    # do calculations and stuff here
    return # an array reshaped(cols,rows) you want the color map to be  

def update(data):
    return mat 

def data_gen():
    while True:
        yield generate_data()

fig, ax = plt.subplots()
mat = ax.matshow(generate_data())
ani = animation.FuncAnimation(fig, update, data_gen, interval=500,
5/23/2017 12:24:25 PM

I suspect it is the use of np.histogram2d in each loop iteration. or that in each loop iteration of the for loop you are clearing and drawing a new figure. To speed things up you should create a figure once and just update the properties and data of the figure in a loop. Have a look through the matplotlib animation examples for some pointers on how to do this. Typically it involves calling matplotlib.pyploy.plot then, in a loop, calling axes.set_xdata and axes.set_ydata.

In your case however, take a look at the matplotlib animation example dynamic image 2. In this example the generation of data is separated from the animation of the data (may not be a great approach if you have lots of data). By splitting these two parts up you can see which is causing a bottleneck, numpy.histrogram2d or imshow (use time.time() around each part).

P.s. np.random.randn is a psuedo-random number generator. These tend to be simple linear generators which can generate many millions of (psuedo-)random numbers per second, so this is almost certainly not your bottleneck - drawing to screen is almost always a slower process than any number crunching.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow