【问题标题】:3D scatter plot animation3D散点图动画
【发布时间】:2021-03-05 17:47:36
【问题描述】:

我正在尝试创建一个 3D 动画散点图,其中每个点都绘制为一个半径为 r 与值 M 成比例的球体(请参阅下面的代码),我想应该通过使用参数 s 来完成ax.scatter,但由于这个值对于每个 (x,y,z) 都是唯一的,我不知道如何将它传递给接受 (x,y,z) 元组的graph._offsets3d。这是任务的第一部分,另一部分是数据应该在他们特定的时间出现t(请看下面的代码)。

  1. 我目前正在努力根据M中的对应值更改每个点的大小,并用对应的时间t对点进行颜色编码,你知道我该怎么做吗?

  2. 我的下一个任务是向图形添加播放/暂停按钮并能够旋转图形吗?

有没有人有类似的经历可以让我受益? 非常感谢!

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

#####Data Generation####

# Space Coordinate
X = np.random.random((100,)) * 255 * 2 - 255
Y = np.random.random((100,)) * 255 * 2 - 255
Z = np.random.random((100,)) * 255 * 2 - 255


# Magnitude of each point
M = np.random.random((100,))*-1+0.5

# Time
t = np.sort(np.random.random((100,))*10)

#ID each point should be color coded. Moreover, each point belongs to a cluster `ID`
ID = np.sort(np.round([np.random.random((100,))*5]))

def update_lines(num):
    for i in range (df_IS["EASTING [m]"].size):
        dx = X[i]
        dy = Y[i]
        dz = Z[i] 
        text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i]))  # for debugging
        x.append(dx) 
        y.append(dy) 
        z.append(dz) 
        graph._offsets3d = (x, y, z) 
        return graph,




fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(X, Y, Z, color='orange')  # s argument here 
text = fig.text(0, 1, "TEXT", va='top')  # for debugging

ax.set_xlim3d(X.min(), X.max())
ax.set_ylim3d(Y.min(), Y.max())
ax.set_zlim3d(Z.min(),Z.max())

# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=500, blit=False)
plt.show()

【问题讨论】:

    标签: python matplotlib animation scatter


    【解决方案1】:

    在动画函数中被数据帧的大小循环了,但是重写了你的代码,部分原因是动画参数与帧数相关联。如果我错了,请纠正我。您也可以使用graph.set_sizes() 传递大小,您可以在此处指定。你的 size 变量有一个负值,所以我将它重新创建为一个整数。由于我的工作环境,我使用了一个单独的库。

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.animation as animation
    from IPython.display import HTML # Animation on jupyter lab 
    from matplotlib.animation import PillowWriter # For GIF animation 
    #####Data Generation####
    
    # Space Coordinate
    X = np.random.random((100,)) * 255 * 2 - 255
    Y = np.random.random((100,)) * 255 * 2 - 255
    Z = np.random.random((100,)) * 255 * 2 - 255
    
    # Magnitude of each point
    # M = np.random.random((100,))*-1+0.5
    M = np.random.randint(1,70, size=100)
    # Time
    t = np.sort(np.random.random((100,))*10)
    
    #ID each point should be color coded. Moreover, each point belongs to a cluster `ID`
    ID = np.sort(np.round([np.random.random((100,))*5]))
    
    x = []
    y = []
    z = []
    m = []
    
    def update_lines(i):
    #     for i in range (df_IS["EASTING [m]"].size):
        dx = X[i]
        dy = Y[i]
        dz = Z[i]
        dm = M[i]
    #     text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i]))  # for debugging
        x.append(dx) 
        y.append(dy) 
        z.append(dz)
        m.append(dm)
        graph._offsets3d = (x, y, z)
        graph.set_sizes(m)
        return graph,
    
    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(111, projection="3d")
    graph = ax.scatter(X, Y, Z, s=M, color='orange')  # s argument here 
    text = fig.text(0, 1, "TEXT", va='top')  # for debugging
    
    ax.set_xlim3d(X.min(), X.max())
    ax.set_ylim3d(Y.min(), Y.max())
    ax.set_zlim3d(Z.min(), Z.max())
    
    # Creating the Animation object
    ani = animation.FuncAnimation(fig, update_lines, frames=100, interval=500, blit=False, repeat=False)
    # plt.show()
    ani.save('test3Dscatter.gif', writer='pillow')
    plt.close()
    HTML(ani.to_html5_video())
    

    编辑:

    # Time
    t = np.sort(np.random.random((100,))*10)
    
    # datapoint for color
    cm_name = 'jet'
    cm = plt.get_cmap(cm_name, 100)
    C = [cm(n) for n in range(cm.N)]
    
    # list for colors add
    x = []
    y = []
    z = []
    m = []
    c = []
    
    # animation function update
    
    dm = M[i]
    dc = C[i] # update
    
    m.append(dm)
    c.append(dc) # update
    
    graph._facecolor3d = c # scatter color defined
    return graph,
    

    【讨论】:

    • 谢谢,这看起来很棒。另一件事是我需要为每个点添加颜色并根据向量 t 对它们进行颜色编码。您是否知道graph.set_color 之类的东西?
    • 运行上面的代码我遇到“RuntimeError: Requested MovieWriter (ffmpeg) not available”。我运行了库导入,并且都安装了它们,并且在这方面没有错误。我在这里错过了什么吗?
    • ax.invert_zaxis()我认为这会反转z轴。在set_zlim3d()之后添加这个。
    • 要为 3d 散点图添加颜色,请像准备任何其他数据一样准备颜色数据。我们在“喷射”色图中准备了 100 种颜色。我们在动画函数中为其准备变量并按顺序添加颜色。重点是指定 3D 颜色。 graph._facecolor3d = c
    • 在r-beginners,感谢您的回复。理想情况下,我希望将所有时间数据归一化,并将颜色映射到具有固定颜色条的点。现在我认为代码会从jet 中随机生成 100 种颜色,这不会欣赏颜色中的时间序列。像this post 这样带有适当颜色条的东西会很棒。我添加了graph = ax.scatter(X, Y, Z, s=M, c=0.5*t, cmap=plt.cm.magma)fig.colorbar(graph, ax=ax),它不仅仅是通过动画
    猜你喜欢
    • 2013-02-02
    • 2012-07-07
    • 2017-04-14
    • 2021-10-21
    • 2019-12-20
    • 2019-12-28
    • 1970-01-01
    • 2023-03-26
    • 2021-11-05
    相关资源
    最近更新 更多