matplotlib圆,动画,如何去除动画中的旧圆 [英] matplotlib circle, animation, how to remove old circle in animation

查看:36
本文介绍了matplotlib圆,动画,如何去除动画中的旧圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to simulate an animation of a circular shock wave from a selected data point using Matplotlib's circle and animation. But I couldn't figure out a way to remove the older circles in every new frame. As a result, as the radius increases, I got more and more circles drawing on the canvas - like this:

关于在 matplotlib 绘图上制作圆形冲击波动画有什么建议吗?

Any advice on animating a circular shockwave on a matplotlib plot?

到目前为止,我的代码是:

The code I have so far is :

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 来改变圆的大小:

I think you can just use set_radius to change the circle size every time:

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()

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

So you are not removing and redrawing patches every round, which I think may be more efficient.

这篇关于matplotlib圆,动画,如何去除动画中的旧圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆