如何在matplotlib

时间:2019-08-04 18:21:10

标签: python matplotlib

我在另一个question post中总结了我的问题,但是在该帖子中没有提供任何代码,因此我打开了这个新的问题帖子以符合社区规则。基本上,我想使用matplotlib在两个图中的两个轴上设置一些圆,点和线的动画。

这是我编写的代码的一部分,

def genData():
    # generate circles, points and distances
    i = 0
    while i < 100:
        i += 1
        # codes to generate some data
        # circle has shape (3,), that is, (x, y, r) 
        # observations have shape (10, 2), representing ten 2D points
        # distance (between circle center and origin) is a double
        yield circle, observations, distance

def update(data):
    c, observations, h = data
    # draw the sampled points
    points.set_offsets(observations[:, 0:2]) # this has no problem

    # draw the estimated circle centers
    center.set_data(c[0], c[1]) # this has no problem

    # draw the estimated circles
    ax1.add_artist(plt.Circle(
        (c[0], c[1]), c[2], color='blue', fill=False))  # this HAS problems

    # draw the line in the right axis
    hs.append(h)
    hs_x = np.arange(len(hs))
    line.set_data(hs_x, hs) # this has no problem

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_aspect('equal')

center, = ax1.plot([], [], 'bo', ms=5) # this has no problem
points = ax1.scatter([], [], c='green') # this has no problem

hs = []
line, = ax2.plot([], [], 'b-', lw=2) # this has no problem

ani = animation.FuncAnimation(fig, update, genData, blit=False, interval=10,
                              repeat=True)
plt.show()

现在,可以正确更新中心和点,即在每帧中将显示新的中心和新点。但是圆圈是累积的,请看到粗的蓝色边缘:因为我将ax1.add_artist(plt.Circle((c[0], c[1]), c[2], color='blue', fill=False))放在了update(data)函数中,但是我不知道该将线条放在哪里或如何放置修复它以满足我的要求(在任何框架下,只会显示一个圆圈)。我尝试将这行circles = ax1.add_artist(plt.Circle((0,0)))放在update()center, = ax1.plot(...)之后的points = ax1.scatter(...)函数之外,但是当我尝试在update(data)函数中绘制新圆时像这样的circles.add_artist(plt.Cirlce((c[0], c[1]), c[2], color='blue', fill=False)),这也是行不通的,因为circles没有名为add_artist的方法。

如何在现有Artist对象中更新Axes对象?

另一个问题是,即使我在interval中将100.1设置为animation.FuncAnimation(),图的渲染似乎也很慢,渲染速度并没有增加100倍如何加快绘图的绘制速度?

0 个答案:

没有答案