【发布时间】:2021-03-05 17:47:36
【问题描述】:
我正在尝试创建一个 3D 动画散点图,其中每个点都绘制为一个半径为 r 与值 M 成比例的球体(请参阅下面的代码),我想应该通过使用参数 s 来完成ax.scatter,但由于这个值对于每个 (x,y,z) 都是唯一的,我不知道如何将它传递给接受 (x,y,z) 元组的graph._offsets3d。这是任务的第一部分,另一部分是数据应该在他们特定的时间出现t(请看下面的代码)。
-
我目前正在努力根据
M中的对应值更改每个点的大小,并用对应的时间t对点进行颜色编码,你知道我该怎么做吗? -
我的下一个任务是向图形添加播放/暂停按钮并能够旋转图形吗?
有没有人有类似的经历可以让我受益? 非常感谢!
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