Animating an xarray dataarray

397 views
Skip to first unread message

Michael Aye

unread,
Apr 28, 2016, 10:02:49 PM4/28/16
to xarray
Did anybody successfully create animations using a 3D (time and 2 spatial dimensions) xarray.Dataset ?

I am able to setup things manually, but I like how the xarray plotting routines set the titles correctly and don't want to rewrite that stuff.

With the help from this I have this so far:

class UpdateQuad(object):

    def __init__(self, ax, data):
        self.ax = ax
        self.data = data
        self.quad = data[0].plot(vmax=72)

    def init(self):
        print('update init')
        self.quad.set_array(np.asarray([]))
        return self.quad

    def __call__(self, i):
        # data at time i
        ti = self.data[i]
        self.quad.set_array(ti.data.ravel())
        return self.quad


fig, ax = plt.subplots()
ud = UpdateQuad(ax, fuv.data)
anim = animation.FuncAnimation(fig, ud, init_func=ud.init,
                               frames=10, blit=False)
anim.save('spectrograms.mp4')


For reasons that are unclear to me, I am not able to make this simply with an update function and on root scripting level.
Now the gripes: If I run it with this __call__() method, I get the right data into the animation, but obviously the title is not being updated, as I only set the data array of the QuadMesh to the new data.

With this __call__() method:

    def __call__(self, i):
        # data at time i
        ti = self.data[i]
        self.ax.clear()
        return ti.plot(ax=self.ax)

I should in principle get the data with the titles updated, but it seems that the ax.clear() does not remove the colorbar, so that I get a new colorbar each time, even so I return a QuadMesh object to the animator (see here)

Would somebody have an idea how to solve this?

Cheers,
Michael


Clark Fitzgerald

unread,
Apr 30, 2016, 11:47:07 AM4/30/16
to xarray
It would be nice to have this functionality included with xarray, and we've thought about it a little. With an animation you probably want the colorbar to stay the same throughout the animation to better show the change over time. Implementing it might be similar to the FacetGrid object. Short term you could pass `add_colorbar=False` to the plot function, and manually pass the same colormap `cmap` around to keep the values the same.

BTW, the links appear to be broken.

Clark
Reply all
Reply to author
Forward
0 new messages