Fast updating Matplotlib plots

The basic structure for a rapidly updating animated plot with Matplotlib, without using matplotlib.animation is described below for imshow() and pcolormesh().

NOTE: matplotlib.pyplot.draw() only takes effect on the CURRENT (most recently called) axes. If updating multiple axes, call draw() after each set of axes is modified.

Both examples assume:

from matplotlib.pyplot import figure, draw, pause

# imgs is a N x X x Y image stack

fg = figure()
ax = fg.gca()

If the pause() statement time is too small, Matplotlib may not update the plot at all. Consider your display may not be able to update faster than about pause(0.01).

More examples with speed comparison showing that imshow is faster than pcolormesh generally.

imshow:

h = ax.imshow(imgs[0])  # set initial display dimensions

for img in imgs:
    h.set_data(img)
    draw(), pause(1e-3)

pcolormesh:

h = ax.pcolormesh(imgs[0])  # set initial display dimensions

for img in imgs:
    h.set_array(img.ravel())
    draw(), pause(1e-3)

Animated plots in Matlab / Octave