On Thu, Nov 3, 2011 at 12:51 PM, Stuart Mumford <stu...@mumford.me.uk>wrote:
> Hello,
>
> I discovered the (wonderful) animation module in the v1.1 release today.
>
> However I think I may have quickly outgrown it a bit!
>
> I am plotting some simulation data see:
> http://dl.dropbox.com/u/2796140/mhdmodes_3.mp4 for an example made with
> the animation class.
>
> There is one thing missing from the above, which is colour bars!
>
> I have created the animation using the ArtisitAnimation class because each
> frame takes about 1-2 seconds to calculate the magnetic field lines, so
> using FuncAnimate would result in a very slow animation.
>
> The problem is that I can not add a colorbar instance to the list of
> artists that the animator draws as it is not an artist instance.
>
> This is a slightly modified version of the example [
> http://matplotlib.sourceforge.net/examples/animation/dynamic_image2.html]
> that shows my problem:
>
> #!/usr/bin/env python
>>
>> """
>>
>> An animated image
>>
>> """
>>
>> import numpy as np
>>
>> import matplotlib.pyplot as plt
>>
>> import matplotlib.animation as animation
>>
>> fig = plt.figure()
>>
>> def f(x, y):
>>
>> return np.sin(x) + np.cos(y)
>>
>> x = np.linspace(0, 2 * np.pi, 120)
>>
>> y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
>>
>> # ims is a list of lists, each row is a list of artists to draw in the
>>
>> # current frame; here we are just animating one artist, the image, in
>>
>> # each frame
>>
>> ims = []
>>
>> for i in range(60):
>>
>> x += np.pi / 15.
>>
>> y += np.pi / 20.
>>
>> im = plt.imshow(f(x, y))
>>
>> cb = plt.colorbar()
>>
>> ims.append([im, cb])
>>
>> ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
>>
>> repeat_delay=1000)
>>
>> plt.show()
>>
>>
> Which generates the following error
>
>> Traceback (most recent call last):
>> File "/home/stuart/Documents/VAC/sac_anim_axtest.py", line 28, in
>> <module>
>> repeat_delay=1000)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 356, in __init__
>> TimedAnimation.__init__(self, fig, *args, **kwargs)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 304, in __init__
>> Animation.__init__(self, fig, event_source=event_source, *args,
>> **kwargs)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 53, in __init__
>> self._init_draw()
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 363, in _init_draw
>> artist.set_visible(False)
>> AttributeError: Colorbar instance has no attribute 'set_visible'
>>
>
>
>
> I attempted to add the colorbar to a AxesSubplot instance and then draw
> the axes in the artist list, however when I add a Axes instance to the
> artist list I get a wonderful error:
>
>
> Traceback (most recent call last):
>> File
>> "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py",
>> line 122, in _on_timer
>> TimerBase._on_timer(self)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py",
>> line 1092, in _on_timer
>> ret = func(*args, **kwargs)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 315, in _step
>> still_going = Animation._step(self, *args)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 177, in _step
>> self._draw_next_frame(framedata, self._blit)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 197, in _draw_next_frame
>> self._post_draw(framedata, blit)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 220, in _post_draw
>> self._blit_draw(self._drawn_artists, self._blit_cache)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/animation.py", line
>> 235, in _blit_draw
>> a.axes.draw_artist(a)
>> File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line
>> 1994, in draw_artist
>> assert self._cachedRenderer is not None
>> AssertionError
>>
>
> I also saw the subplots animation example [
> http://matplotlib.sourceforge.net/examples/animation/subplots.html],
> however that is also based on FuncAnimate, would there be a way to create
> something similar for ArtistAnimate?
>
> Thanks
> Stuart
>
>
Do you need the colorbar scale to change with each frame? If not, you can
create a wholly independent colorbar like so:
cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm)
where 'cax' I usually get by using the axes_grid1 toolkit to pre-allocate a
spot for a colorbar axes (as opposed to using fig.add_subplots()).
However, if you need to the colorbar to change with the frames, then I
would use the FuncAnimator, but a little differently than you are probably
imagining. The fig.colorbar() function requires a ScalarMappable object
(such as an object from imshow()). What I would do is "prime the pump" by
creating the first frame. Then create a func animator that calls a
function that loads the cached results based on an index number and
directly update the imshow()'s object using the set_data() method. That
way, the original colorbar sees the changes from the ScalarMappable object
that was originally assigned to it. It also results in faster rendering,
in my experience since imshow() has many extra steps to take to prepare the
data for display.
Note, this won't work if the domain changes over time.
Does that help?
Ben Root
>
> ------------------------------------------------------------------------------
> RSA(R) Conference 2012
> Save $700 by Nov 18
> Register now
> http://p.sf.net/sfu/rsa-sfdev2dev1
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users