I suspect this is using np.histogram2d in every iteration of the loop. or that in each iteration of the for loop, you clear and draw a new drawing. To speed things up, you need to create a picture once and just update the properties and data of the figure in a loop. Check out the matplotlib animation examples for some pointers on how to do this. It usually 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 example animation matplotlib dynamic image 2 . In this example, data generation is separate from data animation (maybe this is a great approach if you have a lot of data). Dividing the two parts upwards, you will see what causes the bottleneck, numpy.histrogram2d or imshow (use time.time() around each part).
Ps np.random.randn is a pseudo random number generator. They are usually simple linear generators that can generate many millions (psuedo-) random numbers per second, so this is almost certainly not your bottleneck. The pattern on the screen is almost always slower than any number of crunches.
Chris source share