以一维数组作为颜色图的 3D 正态分布散点图

3D normal distribution scatter plot with 1D array as color map

我想创建 3d 散点图,颜色图范围从 min(u),u =64 到 max(u),u=100。你是一维数组

代码按预期工作,u 从中心开始增加 (x,y,z)=(0,0,0) 但颜色不正确,颜色渐变的范围应根据 u,从 min( u) 到 max(u) 而不是取决于 x,y,z 坐标。 colorbar 也不正确(应该从 0 到 100)

fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(111, projection='3d')
ax.set_title('normal distribution')

#add the line/data in our plot
x = 18 * np.random.normal(size =500)
y = 18 * np.random.normal(size =500)
z = 18 * np.random.normal(size =500)

u = np.linspace(64, 100, 500)

norma = mpl.colors.Normalize(min(u), max(u))
color = np.linalg.norm([x,y,z], axis=0)
track = ax.scatter(x,y,z, s=35, c = color, alpha = 1, cmap='inferno', norm = norma)

plt.colorbar(track, label='color map', shrink=0.6)
fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(111, projection='3d')
ax.set_title('normal distribution')

the above code figure

当颜色图归一化为 vmin=min(u) 和 vmax=max(u) 时,颜色渐变丢失,颜色图渐变值沿 x、y、z 轴随机分布,而不是按顺序排列大批。 有人知道如何修复沿轴的颜色渐变,而 u 的中心位于 (0,0,0) 且颜色条 (0-100) 正确吗?

fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(111, projection='3d')
ax.set_title('normal distribution')

#add the line/data in our plot
x = 18 * np.random.normal(size =500)
y = 18 * np.random.normal(size =500)
z = 18 * np.random.normal(size =500)

u = np.linspace(100, 64, 500)

norma = mpl.colors.Normalize(vmin=0, vmax = 100)
color = np.linalg.norm([u], axis=0)
track = ax.scatter(x,y,z, s=35, c = color, alpha = 1, cmap='inferno', norm = norma)

plt.colorbar(track, label='color map', shrink=0.6)

The result of the second example

x = 18 * np.random.normal(size =500)
y = 18 * np.random.normal(size =500)
z = 18 * np.random.normal(size =500)

# collect all data in array
data = np.array([x,y,z])

# center in a given dimension is the mean of all datapoints:
# reshape to allow easy subtraction
center = np.mean(data, axis=1).reshape(3,-1)
# for each datapoint, calculate distance to center and use as color value
color = np.linalg.norm(data - center, axis=0)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

track = ax.scatter(x,y,z, s=35, c = color, alpha = 1, cmap='inferno')
plt.colorbar(track, label='color map', shrink=0.6)

我发现 this question 似乎可以回答您关于坐标的问题。如果您愿意,答案还显示了如何均匀分布坐标。

得到坐标后,你就可以得到离中心的距离作为颜色值(就像warped在他的回答中所做的那样)。我调整了距离以反映您的规格。这是生成的代码:

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

number_of_particles = 500
sphere_radius = 18

# create the particles
radius = sphere_radius * np.random.uniform(0.0, 1.0, number_of_particles)
theta = np.random.uniform(0., 1., number_of_particles) * 2 * np.pi
phi = np.random.uniform(0., 1., number_of_particles) * 2 * np.pi
x = radius * np.sin(theta) * np.cos(phi)
y = radius * np.sin(theta) * np.sin(phi)
z = radius * np.cos(theta)

# collect all data in array
data = np.array([x, y, z])

# for each datapoint, calculate distance to center and use as color value
color = radius
color /= sphere_radius
color = color * 36 + 64

# initialize a figure with a plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# add the points and the colorbar
track = ax.scatter(x, y, z, s=35, c=color, alpha=1, cmap='inferno',
                   norm=Normalize(0, 100))
plt.colorbar(track, label='color map', shrink=0.6)

plt.show()

我的结果是这样的: