且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使matplotlib draw()仅显示新点

更新时间:2023-12-04 12:57:22

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()

原则上,您也可以使用普通的绘图代替 scatter 并在循环中更新数据,但是3D更新可能会很不稳定,可能需要稍微戳一下内部。

In principle you can also use a normal plot instead of scatter and update the data in the loop, but the 3D updating can be wonky/may require poking at the internals a bit.