python-3.x - 如何根据动画图中的发生时间对 3D 散点图进行颜色编码?
问题描述
我开发了一个代码来创建一个动画散点图。关于数据集,我有每个点的 X、Y、Z 坐标,每个事件点都分配了一个值 (M),每个事件点都发生在特定时间 (t)。我让每个点的大小与它们的值(即 M)成正比,现在我想为每个点添加颜色,以便它也显示发生的时间。我知道我必须使用.set_color(c)
但c
value 需要一个元组值。我试图标准化时间值以映射此帖子中的颜色。但是,我想念一些东西,因为代码无法为相关时间的点着色。如果有人可以分享他们的经验,我将不胜感激?
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())
解决方案
您需要将“颜色”更改为“cmap”,以便能够调用颜色集,见下文:
graph = ax.scatter(X, Y, Z, s=M, cmap='jet') #jet is similar to rainbow
推荐阅读
- python - 如何使用 Python 制作 Minecraft Java 深度学习机器人
- android - 如何在调用 google pay API 时了解我的支付提供商名称?
- cmake - 在 CMake (cmake-gui) 中使用 ComboBoxes 约束值
- android - 关闭 postDelayed 处理程序
- c - C 中 const 关键字的歧义
- java - 为什么我的 java 构建使用 Maven 失败?
- asp.net - Visual Studio 2019--HRESULT 0x80041fe4 异常
- javascript - 反应复选框放置方法
- php - 提高 Laravel 中多行插入的性能
- node.js - 云功能的执行时间是否会激增?