我刚刚开始动画我的matplotlib补丁。在我目前的工作中,我想显示一个可视化,在那里有一个楔形,可以随着时间的推移改变它的大小。然而,我得到了一个静态的楔子,原因超出了我的视野。
#!/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()我尝试了用一个循环补丁来代替patch_one,它确实起了作用(在代码中注释掉了这个部分)。
发布于 2014-08-08 17:56:43
代码似乎需要两个小的修改:
# animation function. This is called sequentially
def animate(i):
patch_one.r +=i*0.0001
patch_one._recompute_path()
return patch_one,为什么:
r定义半径,没有属性radius。另一方面,您可以(而且可能应该)使用set_radius()方法。_recompute_path来重新计算实际路径。后者似乎是源中的遗漏,set_radius可以调用_recompute_path的方法。然而,也有一些更聪明的人(至少@tcaswell)能够分辨这是一个bug还是其他的事情。
(顺便问一下,你确定要说patch_one.r += i*0.0001吗?这个动画看上去有点奇怪。也许你是说patch_one.r = 1 + i*.0001?)
https://stackoverflow.com/questions/25207432
复制相似问题