matplotlib圆,动画,如何删除动画中的旧圆

发布于 2021-01-29 14:10:19

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

在此处输入图片说明

关于在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()
关注者
0
被浏览
187
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    我认为您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()
    

    因此,您不必每次都删除和重绘补丁,我认为这样做可能更有效。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看