gpt4 book ai didi

python - 如何将固定颜色条添加到 3D 散点动画图并相应地对点进行颜色映射?

转载 作者:行者123 更新时间:2023-12-03 20:40:02 26 4
gpt4 key购买 nike

我花了相当多的时间阅读了很多关于如何生成动画散点图以及如何对它们进行颜色编码的帖子。理想情况下,我想要 X、Y、Z 坐标的散点图,每个散点图根据其对应的 M 值和颜色根据其对应的 t 值具有大小。颜色代码必须根据向量 t 的最大和最小时间从头开始固定,并且点以与其时间值(即 t 向量)相关联的颜色出现。帖子的最后一部分(最后的代码)能够根据 M 生成不同大小但只有一种颜色的散点图。
更多详情:关于数据集,我有每个点的 X、Y、Z 坐标,每个事件点都分配了一个值 (M),每个都发生在特定时间 (t)。
我让每个点的大小与它们的值(即 M)成正比,现在我想为每个点添加颜色,以便它也显示出现的时间。
解决方案(1)我试过:我知道我必须使用 .set_color(c)但是 c value 需要一个元组值。我试图标准化时间值以映射来自 this post 的颜色.但是,我想念一些东西,因为代码无法根据相关时间为点着色。我使用了以下代码

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)



x = []
y = []
z = []
m = []
cmap = plt.cm.Spectral
norm = plt.Normalize(vmin=min(t), vmax=max(t))
# plt.scatter(x,y, c = cmap(norm(t)))
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, c = cmap(norm(t))) # 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=30, interval=500, blit=False, repeat=False)
# plt.show()
ani.save('test3Dscatter.gif', writer='pillow')
plt.close()
HTML(ani.to_html5_video())
解决方案(2)我试过:我也看了 this post ,基于这篇文章,我补充说:
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####
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)


x = []
y = []
z = []
m = []
t = []
###=====edited part=====####
def get_colour(t):
'''
with this function I tried to generate a new color for each point and then use the graph.set-
color to plot it but it is not successful
'''
cmap = matplotlib.cm.get_cmap('Spectral');
return cmap(t);

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]
dt = t[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)
t.append(dt)
graph._offsets3d = (x, y, z)
graph.set_sizes(m)
graph.set_color(c=get_colour(t))
return graph,
如果有人可以分享他们的经验,并了解如何将颜色条和颜色映射点添加到图形中,我将不胜感激?
这是生成具有不同大小但颜色统一的 3D 散点图的代码。
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=30, interval=500, blit=False, repeat=False)
# plt.show()
ani.save('test3Dscatter.gif', writer='pillow')
plt.close()
HTML(ani.to_html5_video())

最佳答案

frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
cbar = fig.colorbar(frame)

For more Details refer this link :How to animate the colorbar in matplotlib

关于python - 如何将固定颜色条添加到 3D 散点动画图并相应地对点进行颜色映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67352123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com