2015-08-14 129 views
2

我想模拟使用Matplotlib的圆和动画从选定的数据点的圆形激波的动画。 但我无法想出一种方法来删除每个新框架中的旧圈子。其结果是,随着半径增大,我得到了越来越多的圈在画布上绘制 - 这样的:matplotlib圈,动画,如何删除动画中的旧圆

enter image description here

上一个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() 

回答

3

我想你可以只使用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() 

所以你不删除,重划补丁每一轮,我想这可能是更有效的。

+0

谢谢... set_radius正是我所需要的。非常感激。 –

+0

嗨,再次。新代码工作正常,但不在ipython笔记本上。它似乎崩溃了内核。我用%pylab tk弹出了plt窗口。你认为这与此有关吗? –

+0

可能是传统知识问题?如果你不在ipython笔记本上,它有效吗?我使用OSX后端,根本没有得到这个问题。 –

1

将此代码添加到您的动画功能:

for obj in ax.findobj(match = type(plt.Circle(1, 1))): 
    obj.remove() 

ax.findobj(match = type(plt.Circle(1, 1)))查找轴线上的所有圆圈的补丁,然后你就可以删除然后绘制一个新的圈子。我使用plt.Circle()来创建一个圆形对象来匹配 - 通过它的参数并不重要。