我刚刚开始为我的 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 来尝试代码,它确实有效(在代码中留下了注释掉的那部分)。