以颜色作为第四个变量的 3D 点散布

问题描述 投票:0回答:1

我正在尝试以 3D 形式分散数据集,并使用标量变量作为颜色为每个点分散数据,以更好地可视化某些现象。 为了简单起见,假设我希望绘制的每个点都有一种颜色,以某种方式代表另一个 3D 数组(密度、温度、能量等)。

我该怎么做?我想要类似以下的东西:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#some dummy data
x = np.linspace(-1,1,3, endpoint = True )
y  = np.linspace(-1,1,3, endpoint = True )
z = np.linspace(-1,1,3, endpoint = True )
X,Y,Z = np.meshgrid(x,y,z,indexing = 'ij')
E = X**2+Y**2+Z**2
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
ax.scatter(X,Y,Z, color = VV)
plt.show()

这里 VV 是一个数组,在某种程度上与 E 成正比,并且根据 E[x,y,z] 为每个 (x,y,z) 提供颜色。 最后一部分是放置一个颜色条来帮助读取近似颜色值。 我已经在论坛上搜索过,但我找到的答案不适用于 3D 数据集,我目前陷入困境。

python matplotlib matplotlib-3d
1个回答
0
投票

如果我已经理解了这个问题,您只需在对

c=E
的调用中设置
scatter
,Matplotlib 就会自动将其标准化以适应色标。
scatter
返回
PathCollection
,它是一种可映射类型,因此可以传递给
colorbar

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#some dummy data
x = np.linspace(-1,1,3, endpoint = True )
y  = np.linspace(-1,1,3, endpoint = True )
z = np.linspace(-1,1,3, endpoint = True )
X,Y,Z = np.meshgrid(x,y,z,indexing = 'ij')
E = X**2+Y**2+Z**2

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
pc = ax.scatter(X, Y, Z, c=E)
fig.colorbar(pc)
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.