首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib圆、动画、如何去除动画中的老圆

matplotlib圆、动画、如何去除动画中的老圆
EN

Stack Overflow用户
提问于 2015-08-14 15:38:09
回答 2查看 4.9K关注 0票数 4

我试图用Matplotlib的圆和动画从选定的数据点模拟一个圆形激波的动画。但我想不出在每一个新的画面中消除旧的圆圈的方法。因此,随着半径的增加,我在画布上画了越来越多的圆圈,如下所示:

对于在matplotlib图上动画圆形激波有什么建议吗?

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

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-14 15:59:51

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

代码语言:javascript
复制
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()

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

票数 5
EN

Stack Overflow用户

发布于 2015-08-14 15:52:37

将此代码添加到动画函数中:

代码语言:javascript
复制
for obj in ax.findobj(match = type(plt.Circle(1, 1))):
  obj.remove()

ax.findobj(match = type(plt.Circle(1, 1)))查找轴上的所有圆补丁,然后在绘制新的圆之前将其移除。我使用plt.Circle()来创建一个圆形对象来匹配--传递给它的参数并不重要。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32013763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档