I think it is because the figure may or may not have some things drawn by
the time the blitting starts. This is due to draw_idle(). So, it is trying
to capture whatever is in the figure's canvas, but drawing may or may not
have happened yet.
Try this:
def animate(i):
if not animate.patch:
animate.patch = plt.Circle((5, -5), 0.75, fc='y')
animate.ax.add_patch(animate.patch)
x, y = animate.patch.center
x = 5 + 3 * np.sin(np.radians(i))
y = 5 + 3 * np.cos(np.radians(i))
animate.patch.center = (x, y)
return animate.patch,
animate.ax = ax
animate.patch = None
If you have something more complicated, then just go full bore and use
classes to store the state.
Cheers!
Ben Root
On Wed, Apr 23, 2014 at 10:51 AM, Raymond Smith <smit...@mit.edu> wrote:
> This is pretty weird. If instead of Mark's original script, if I move the
> add_patch out of init and have the init simply return an empty tuple, it
> _mostly_ works as expected. But -- at least on my computer -- on some runs,
> it has the moving circle, but also leaves a circle at the "top", starting
> point, whereas on other runs it simply has the desired moving circle with
> no 'background' circle. Usually, it will happen at least once if I start
> the animation script 10 times. So still, the init function is a bit of a
> mystery to me.
>
>
> import numpy as np
> from matplotlib import pyplot as plt
> from matplotlib import animation
>
> fig = plt.figure()
> fig.set_dpi(100)
> fig.set_size_inches(7, 6.5)
>
> ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
> patch = plt.Circle((5, -5), 0.75, fc='y')
> ax.add_patch(patch)
>
> def init():
> return tuple()
>
>
> def animate(i):
> x, y = patch.center
> patch.set_facecolor('y')
> patch.set_edgecolor('k')
>
> x = 5 + 3 * np.sin(np.radians(i))
> y = 5 + 3 * np.cos(np.radians(i))
> patch.center = (x, y)
> return patch,
>
> anim = animation.FuncAnimation(fig, animate,
> init_func=init,
> frames=360,
> interval=20,
> blit=True)
>
> plt.show()
>
>
>
>
> On Wed, Apr 23, 2014 at 10:29 AM, Benjamin Root <ben.r...@ou.edu> wrote:
>
>> Working off of very faded memory, try not to return any objects in your
>> init function that you intend to be animated. If I remember correctly, when
>> blitting is True, the animator treats any object returned by the init()
>> function as background objects, and any objects returned by the animation
>> function as blittable. Since your patch is returned in both functions, I
>> think it is getting confused.
>>
>> Again, very rusty memory here...
>>
>> Ben Root
>>
>>
>>
>> On Wed, Apr 23, 2014 at 9:34 AM, Raymond Smith <smit...@mit.edu> wrote:
>>
>>> Hi Mark,
>>>
>>> I can't say this is the 'proper' solution or the correct interpretation,
>>> but it should work.
>>>
>>> I think when blitting that the init function serves as a something of a
>>> "background" for the rest of the animation. So try changing
>>>
>>>
>>> def init():
>>> *patch.center = (5, 5)*
>>> ax.add_patch(patch)
>>> return patch,
>>>
>>> to
>>>
>>> def init():
>>> *patch.center = (5, -5)*
>>> ax.add_patch(patch)
>>> return patch,
>>>
>>> Cheers,
>>> Ray
>>>
>>>
>>> On Wed, Apr 23, 2014 at 5:44 AM, Mark Bakker <mark...@gmail.com> wrote:
>>>
>>>> Hello list,
>>>>
>>>> I am trying to animate a patch. The animation should show a circle
>>>> orbiting around a point. I took the code from
>>>> http://nickcharlton.net/posts/drawing-animating-shapes-matplotlib.html
>>>>
>>>> Problem is that when I run the code, the animation doesn't remove the
>>>> initial position of the circle (blit is True) while it works correctly on
>>>> the website referenced above.
>>>>
>>>> Does anybody else see this behavior? Here's the code:
>>>>
>>>> import numpy as np
>>>> from matplotlib import pyplot as plt
>>>> from matplotlib import animation
>>>>
>>>> fig = plt.figure()
>>>> fig.set_dpi(100)
>>>> fig.set_size_inches(7, 6.5)
>>>>
>>>> ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
>>>> patch = plt.Circle((5, -5), 0.75, fc='y')
>>>>
>>>> def init():
>>>> patch.center = (5, 5)
>>>> ax.add_patch(patch)
>>>> return patch,
>>>>
>>>> def animate(i):
>>>> x, y = patch.center
>>>> x = 5 + 3 * np.sin(np.radians(i))
>>>> y = 5 + 3 * np.cos(np.radians(i))
>>>> patch.center = (x, y)
>>>> return patch,
>>>>
>>>> anim = animation.FuncAnimation(fig, animate,
>>>> init_func=init,
>>>> frames=360,
>>>> interval=20,
>>>> blit=True)
>>>>
>>>> plt.show()
>>>>
>>>> Thanks, Mark
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> Start Your Social Network Today - Download eXo Platform
>>>> Build your Enterprise Intranet with eXo Platform Software
>>>> Java Based Open Source Intranet - Social, Extensible, Cloud Ready
>>>> Get Started Now And Turn Your Intranet Into A Collaboration Platform
>>>> http://p.sf.net/sfu/ExoPlatform
>>>> _______________________________________________
>>>> Matplotlib-users mailing list
>>>> Matplotlib-users@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>>>
>>>>
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Start Your Social Network Today - Download eXo Platform
>>> Build your Enterprise Intranet with eXo Platform Software
>>> Java Based Open Source Intranet - Social, Extensible, Cloud Ready
>>> Get Started Now And Turn Your Intranet Into A Collaboration Platform
>>> http://p.sf.net/sfu/ExoPlatform
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>>
>>>
>>
>
------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users