Python - 如何在散点图上显示单个数据点?

时间:2016-08-18 04:11:59

标签: python animation matplotlib

我试图显示3D散点图。下面的代码可以工作,但它显示当前和旧的数据点。我想一次只显示一个。我尝试过不同的选择,但我无法正常使用它。有什么建议吗?

谢谢!

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlim([-1.5, 1.5])
ax.set_ylim([-1.5, 1.5])
ax.set_zlim(0, 1.5)

ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')

data = [[-1.0, -1.0, 0.0], [1.0, -1.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 0.0, 1.0]]

def animate(data):

    x = data[0]
    y = data[1]
    z = data[2]

    scat = ax.scatter3D(x, y, z, c='r', marker='o')

    return

ani = animation.FuncAnimation(fig, animate, data, interval=1000, blit=False, repeat=False)

plt.show()

1 个答案:

答案 0 :(得分:0)

您可以在for循环中清除每次迭代中的轴。请注意,您还需要注意轴限制。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlim([-1.5, 1.5])
ax.set_ylim([-1.5, 1.5])
ax.set_zlim(0, 1.5)

ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')

data = [[-1.0, -1.0, 0.0], [1.0, -1.0, 0.0], [0.0, -1.0, 1.0], [-1.0, 0.0, 1.0]]

def animate(data):

    x = data[0]
    y = data[1]
    z = data[2]

    plt.cla()
    ax.set_xlim([-1.5,1.5])
    ax.set_ylim([-1.5,1.5])
    ax.set_zlim([-1.5,1.5])
    scat = ax.scatter3D(x, y, z, c='r', marker='o')

    return

ani = animation.FuncAnimation(fig, animate, data, interval=1000, blit=False, repeat=False)

plt.show()