【问题标题】:Python - How to display a single data point on a scatter plot?Python - 如何在散点图上显示单个数据点?
【发布时间】:2016-08-18 04:11:58
【问题描述】:

我正在尝试显示 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()

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    您可以在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()
    

    【讨论】:

    • 这似乎只在您不绘制其他任何东西时才有效。例如,我需要绘制一条头部为散点的线。如果我清除轴,我会失去我不想要的行为。
    猜你喜欢
    • 2022-01-13
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多