首页 > 解决方案 > 为什么这个动态更新的散点图没有做任何事情?

问题描述

我正在尝试找到一种动态更新 3d 散点图的方法。我发现这篇关于动态更新的 2d 图的帖子,我能够让代码在我的机器上运行而无需任何调整。但是当试图让它适应 3d 情节时,我遇到了一些麻烦。这是我的代码:

import numpy as np
import matplotlib.animation as ani
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig, sa, sb, l, a, b, z, x = None, None, None, None, None, None, None, None
devices = []
a=2


def animate(i):
    global a
    x.append(a)
    y.append(a)
    z.append(a)
    sc.set_offsets(np.c_[x,y,z])
    a += 1

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d(0, 10)
ax.set_ylim3d(0, 10)
ax.set_zlim3d(0, 10)

plt.subplots_adjust(left=0.25, bottom=0.25)

x = [1]
y = [1]
z = [1]
sc = ax.scatter(x,y,z)

animation = ani.FuncAnimation(fig, animate, frames=2, interval=100)

plt.show()

当我运行它时,会弹出一个 3d 绘图,其中绘制了一个点并且没有动画。任何人都知道这段代码有什么问题吗?谢谢!

标签: pythonmatplotlib

解决方案


推荐阅读