我正在尝试使用 Matplotlib 的圆和动画模拟来自选定数据点的圆形冲击波的动画。
但是我想不出一种方法来删除每个新帧中的旧圆圈。结果,随着半径的增加,我在 Canvas 上绘制了越来越多的圆圈 - 像这样:

python - matplotlib圆,动画,如何去除动画中的旧圆-LMLPHP

关于在 matplotlib 图上为圆形冲击波设置动画的任何建议?

我到目前为止的代码是:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

testData = np.random.rand(100,2)-[0.5, 0.5]

fig, ax = plt.subplots()
def init():
    fig.clf()
    sctPlot, = ax.plot(testData[:,0], testData[:,1], ".")

# i is the radius
def animate(i):
    init()
    q = 20 #initial index for the starting position
    pos = testData[q,:]
    circle1=plt.Circle(pos,i,color='r',fill=False, clip_on = False)
    fig.gca().add_artist(circle1)

def init():
    sctPlot, = ax.plot(testData[:,0], testData[:,1], ".")
    return sctPlot,

ani = animation.FuncAnimation(fig, animate, np.arange(0.4, 2, 0.1), init_func=init,
    interval=25, blit=False)
plt.show()

最佳答案

我认为您可以使用 set_radius 每次更改圆圈大小:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


testData = np.random.rand(100,2)-[0.5, 0.5]

fig, ax = plt.subplots()
circle1=plt.Circle(testData[20,:],0,color='r',fill=False, clip_on = False)
ax.add_artist(circle1)
# i is the radius
def animate(i):
    #init()
    #you don't want to redraw the same dots over and over again, do you?
    circle1.set_radius(i)


def init():
    sctPlot, = ax.plot(testData[:,0], testData[:,1], ".")
    return sctPlot,

ani = animation.FuncAnimation(fig, animate, np.arange(0.4, 2, 0.1), init_func=init,
    interval=25, blit=False)
plt.show()

所以你不是每轮都删除和重绘补丁,我认为这可能更有效。

关于python - matplotlib圆,动画,如何去除动画中的旧圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32013763/

10-12 16:39
查看更多