我想使用matplotlib(python)以3D形式绘制,这些数据是实时添加的(x,y,z)。
在下面的代码中,数据成功附加在 x 轴和 y 轴上,但在 z 轴上我遇到了问题。虽然我在 matplotlib 的文档中搜索过,但我找不到任何解决方案。
应该在此代码中添加/更改什么以使其在 z 轴中附加数据?
什么工作正常:
return plt.plot(x, y, color='g')
问题:
return plt.plot(x, y, z, color='g')
代码:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import random
np.set_printoptions(threshold=np.inf)
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
x = []
y = []
z = []
def animate(i):
x.append(random.randint(0,5))
y.append(random.randint(0,5))
z.append(random.randint(0,5))
return plt.plot(x, y, color='g')
#return plt.plot(x, y, z, color='g') => error
ani = animation.FuncAnimation(fig, animate, interval=1000)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
plt.show()
如何正确完成这项工作?