[英]How to do color code the 3D scatter plot according to time of occurrence in an animated graph?
I have developed a code to create an animated scatter graph.我开发了一个代码来创建一个动画散点图。 About the dataset, I have the X,Y,Z coordinate of each point and each event point are assigned a value (M) and each happened at a specific time (t).
关于数据集,我有每个点的 X、Y、Z 坐标,每个事件点都分配了一个值 (M),每个事件点都发生在特定时间 (t)。 I have the size of each point to be proportional to their value (ie, M), now I want to add the color to each point so that it also shows the time of occurrence.
我让每个点的大小与它们的值(即 M)成正比,现在我想为每个点添加颜色,以便它也显示发生的时间。 I know I have to use
.set_color(c)
but c
value expects a tuple value.我知道我必须使用
.set_color(c)
但c
值需要一个元组值。 I tried to normalize the values of the time to map the color from this post .我试图将时间值标准化为 map 来自这篇文章的颜色。 However, there is something that I miss because the code is not working to color the points with related time.
但是,我错过了一些东西,因为代码无法为相关时间的点着色。 I would appreciate it if someone could share their experiences?
如果有人可以分享他们的经验,我将不胜感激?
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())
You need to change "Color" to "cmap" so that you are able to call set of colors, see below:您需要将“颜色”更改为“cmap”,以便能够调用 colors 的集合,见下文:
graph = ax.scatter(X, Y, Z, s=M, cmap='jet') #jet is similar to rainbow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.