【问题标题】:make matplotlib draw() only show new point使 matplotlib draw() 只显示新点
【发布时间】:2013-05-08 18:35:52
【问题描述】:

所以我有一个 3D 图,它是一个散点图,通过数据框更新一个点。我让它每 0.1 秒添加一个新点。这是我的代码:

ion()
fig = figure()
ax = fig.add_subplot(111, projection='3d')
count = 0
plotting = True
while plotting:
    df2 = df.ix[count]
    count += 1
    xs = df2['x.mean']
    ys = df2['y.mean']
    zs = df2['z.mean']
    t = df2['time']
    ax.scatter(xs, ys, zs)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.set_title(t)
    draw()
    pause(0.01)
    if count > 50:
        plotting = False
ioff()
show()

我怎样才能让它只在实时更新的图表上显示新点。现在它从一个点开始,然后添加另一个点,直到图表上总共有 50 个点。

所以我想要的是图表上永远不会有超过一个点,并且只是让该点在迭代时发生变化。我该怎么做?

【问题讨论】:

    标签: python matplotlib drawing


    【解决方案1】:
    ion()
    fig = figure()
    ax = fig.add_subplot(111, projection='3d')
    count = 0
    plotting = True
    # doesn't need to be in loop
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    
    lin = None
    while plotting:
        df2 = df.ix[count]
        count += 1
        xs = df2['x.mean']
        ys = df2['y.mean']
        zs = df2['z.mean']
        t = df2['time']
        if lin is not None:
            lin.remove()
        lin = ax.scatter(xs, ys, zs)
        ax.set_title(t)
        draw()
        pause(0.01)
        if count > 50:
            plotting = False
    ioff()
    show()
    

    原则上,您也可以使用普通的plot 而不是scatter 并更新循环中的数据,但是 3D 更新可能很不稳定/可能需要稍微检查一下内部结构。

    【讨论】:

    • 作为记录,ax.set_title(t) 确实需要在循环中,因为它可以让我跟踪时间。现在这个点总是在中间,当我这样做时轴标签会改变......我该如何防止这种情况发生?
    • @RyanSaxe opps,抱歉,没有完全了解它在做什么。恢复了那一点。
    • 很抱歉,这有一个问题,所以我没有接受它,尽管它确实适用于这个问题。我发布了一个新问题here
    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多