Fast updating Matplotlib plots

Related: Animated plots in Matlab / Octave


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

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)

Tags:

Categories:

Written by Michael Hirsch, Ph.D. //

Comments