Python 如何将固定颜色栏添加到三维散点动画绘图中,并相应地对点进行颜色映射?

Python 如何将固定颜色栏添加到三维散点动画绘图中,并相应地对点进行颜色映射?,python,animation,scatter-plot,Python,Animation,Scatter Plot,我花了相当多的时间阅读了很多关于如何生成动画散点图以及如何对它们进行颜色编码的帖子。理想情况下,我希望有X,Y,Z坐标的散点图,每个散点图都有一个根据其对应的M值确定的大小和一个根据其对应的t值确定的颜色。颜色代码必须根据向量t的最大和最小时间从一开始就固定,并且点显示的颜色与其时间值(即t向量)相关。文章的最后一部分(末尾的代码)能够根据M生成不同大小的散点图,但只有一种颜色 更多详细信息:关于数据集,我有每个点的X、Y、Z坐标,每个事件点都分配了一个值(M),每个值都发生在特定的时间(t)。

我花了相当多的时间阅读了很多关于如何生成动画散点图以及如何对它们进行颜色编码的帖子。理想情况下,我希望有X,Y,Z坐标的散点图,每个散点图都有一个根据其对应的M值确定的大小和一个根据其对应的t值确定的颜色。颜色代码必须根据向量t的最大和最小时间从一开始就固定,并且点显示的颜色与其时间值(即t向量)相关。文章的最后一部分(末尾的代码)能够根据M生成不同大小的散点图,但只有一种颜色

更多详细信息:关于数据集,我有每个点的X、Y、Z坐标,每个事件点都分配了一个值(M),每个值都发生在特定的时间(t)。 我让每个点的大小与它们的值(即M)成比例,现在我想给每个点添加颜色,以便它也显示发生的时间

解决方案(1)我尝试过:我知道我必须使用
。设置颜色(c)
但是
c
值需要一个元组值。我试图规范化映射颜色的时间值。但是,我遗漏了一些东西,因为代码无法使用相关时间为点着色。我使用了以下代码

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)我尝试过:我也研究过,根据这篇文章,我补充道:

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,
如果有人能分享他们的经验,看看我如何将颜色栏和颜色映射点添加到图形中,我将不胜感激? 下面是生成三维散点图的代码,该散点图大小不同,但颜色一致

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)