我有一个包含补丁的matplotlib图。在定期的时间间隔,我想更改补丁(删除旧补丁并添加另一个补丁)。
这是一般设置。
fig = plt.figure()
ax = fig.add_subplot(111)
path1 = Path(vertices, codes)
patch1 = patches.PathPatch(path1, facecolor='white', lw=0.3, fill=False)
ax.add_patch(patch1)
plt.show()
1秒后,删除补丁1然后
path2 = Path(vertices2, codes2)
patch2 = patches.PathPatch(path2, facecolor='white', lw=0.3, fill=False)
ax.add_patch(patch2)
答案 0 :(得分:0)
您可以使用matplotlib.animation.FuncAnimation
每秒绘制一个新的Polygon。
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np; np.random.seed(1)
def get_vertices():
p = np.random.rand(4,2)*.8+.1
d = p-np.mean(p,axis=0)
s = np.arctan2(d[:,0], d[:,1])
return p[np.argsort(s),:]
fig = plt.figure()
ax = fig.add_subplot(111)
patches = []
kw = dict(edgecolor='darkorange', lw=3, facecolor="peachpuff")
patches.append(plt.Polygon(get_vertices(),**kw))
# of course PathPatch(Path(get_vertices()))
# would also work
ax.add_patch(patches[0])
def update(i):
patches[0].remove()
patches[0] = plt.Polygon(get_vertices(),**kw)
ax.add_patch(patches[0])
ani = matplotlib.animation.FuncAnimation(fig, update, interval=1000)
plt.show()