From 81cf78047d445ad237c50d10f91f7ba2a1ecb074 Mon Sep 17 00:00:00 2001 From: Niru Maheswaranathan Date: Thu, 18 Feb 2016 23:33:50 -0800 Subject: [PATCH] adds play function to chart for animating a stack of images --- jetpack/chart.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/jetpack/chart.py b/jetpack/chart.py index 8934256..5f57825 100644 --- a/jetpack/chart.py +++ b/jetpack/chart.py @@ -9,10 +9,11 @@ # imports import numpy as np import matplotlib.pyplot as plt +import matplotlib.animation as animation from functools import wraps __all__ = ['plotwrapper', 'image', 'hist', 'hist2d', 'errorplot', 'random_slice', - 'setfontsize', 'noticks', 'nospines', 'breathe'] + 'setfontsize', 'noticks', 'nospines', 'breathe', 'play'] def plotwrapper(fun): @@ -140,6 +141,33 @@ def image(data, mode='div', center=True, cmap=None, aspect='equal', vmin=None, v plt.draw() +def play(images, cmap='viridis', interval=50, clim=None, **kwargs): + + fig = plt.figure() + ax = fig.add_subplot(111) + noticks(ax=ax) + + # set up the figure + plt.axis('equal') + img = ax.imshow(images[0]) + + # set up the colormap + img.set_cmap(cmap) + img.set_interpolation('nearest') + if clim is not None: + img.set_clim(clim) + else: + maxval = np.max(np.abs(images)) + img.set_clim([-maxval, maxval]) + + def animate(im): + img.set_data(im) + + anim = animation.FuncAnimation(fig, animate, images, interval=interval) + plt.show() + return anim + + @plotwrapper def corrplot(C, cmap=None, cmap_range=(0.0, 1.0), cbar=True, fontsize=14, **kwargs): """