我试图用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()发布于 2015-08-14 15:59:51
我认为您可以使用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()所以你不是每一轮删除和重绘补丁,我认为这可能会更有效。
发布于 2015-08-14 15:52:37
将此代码添加到动画函数中:
for obj in ax.findobj(match = type(plt.Circle(1, 1))):
obj.remove()ax.findobj(match = type(plt.Circle(1, 1)))查找轴上的所有圆补丁,然后在绘制新的圆之前将其移除。我使用plt.Circle()来创建一个圆形对象来匹配--传递给它的参数并不重要。
https://stackoverflow.com/questions/32013763
复制相似问题