Reputation: 3325
I have just started animating my matplotlib patches. In my current work, I want to display a visualization where there exist a wedge that change it size through time. However, I am getting a static wedge, for a reason beyond my vision.
#!/usr/bin/env python
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.patches
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(-2, 2), ylim=(-2, 2))
plt.grid(True)
# patch_one = matplotlib.patches.Circle((0, 0), 1)
patch_one = matplotlib.patches.Wedge((0,0), 1, -10, 10)
# initialization function: plot the background of each frame
def init():
patch_one.radius = 1
ax.add_patch(patch_one)
return patch_one,
# animation function. This is called sequentially
def animate(i):
patch_one.radius +=i*0.0001
return patch_one,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
plt.show()
I tried the code by replacing patch_one by a circular patch and it did work (left that part commented out in the code).
Upvotes: 1
Views: 1505
Reputation: 23530
The code seems to need two small changes:
# animation function. This is called sequentially
def animate(i):
patch_one.r +=i*0.0001
patch_one._recompute_path()
return patch_one,
Why:
the attribute r
defines the radius, there is no attribute radius
. On the other hand, you may (and probably should) use the method set_radius()
.
For some reason or another, the internal method _recompute_path
needs to be called manually to recompute the actual path.
The latter one seems like an omission in the source, the method set_radius
could call _recompute_path
. However, there are wiser people (at least @tcaswell) who may be able to tell if this is a bug or if there is something else going on.
(By the way, are you sure you want to say patch_one.r += i*0.0001
? The animation looks a bit odd with that. Maybe you mean patch_one.r = 1 + i*.0001
?)
Upvotes: 3