Python 以一维阵列为彩色图的三维正态分布散点图

Python 以一维阵列为彩色图的三维正态分布散点图,python,numpy,matplotlib,colormap,Python,Numpy,Matplotlib,Colormap,我想创建一个三维散点图,颜色范围从最小值(u),u=64到最大值(u),u=100。u是一维阵列 代码按预期工作,u从中心(x,y,z)=(0,0,0)增加,但颜色不正确,颜色渐变应根据u,从最小(u)到最大(u),而不是根据x,y,z坐标。此外,颜色栏不正确(应在0到100之间) 当颜色贴图法线化为vmin=min(u)和vmax=max(u)时,颜色渐变将丢失,并且颜色贴图渐变值将沿x、y、z轴随机分布,而不是按顺序排列。 有人知道如何用正确的颜色条(0-100)在u的中心位于(0,0,0

我想创建一个三维散点图,颜色范围从最小值(u),u=64到最大值(u),u=100。u是一维阵列

代码按预期工作,u从中心(x,y,z)=(0,0,0)增加,但颜色不正确,颜色渐变应根据u,从最小(u)到最大(u),而不是根据x,y,z坐标。此外,颜色栏不正确(应在0到100之间)

当颜色贴图法线化为vmin=min(u)和vmax=max(u)时,颜色渐变将丢失,并且颜色贴图渐变值将沿x、y、z轴随机分布,而不是按顺序排列。 有人知道如何用正确的颜色条(0-100)在u的中心位于(0,0,0)时沿轴固定颜色渐变吗

我找到了似乎能回答你关于坐标的问题的答案。答案还显示了如果您愿意,如何均匀分布坐标

在获得坐标后,您可以将距离中心的距离作为颜色值(就像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()
我的结果如下所示:


我是否正确理解您希望使用
x
y
z
作为坐标,使用
u
作为颜色?那么你的第一个例子是正确的。如果你想规范化颜色,只需使用
min(u)
max(u)
作为
normalize()
的参数。谢谢你的回答,我已经尝试了你的建议,但仍然在随机位置获得颜色,而不是从min(u)到max(u)
norma=mpl.colors.normalize(vmin=min(u),vmax=max(u)
啊,我想我现在明白你的问题了。你希望
u
从中心开始增加。在这种情况下,warped的答案应该很好。你刚刚将线性增加的
u
分配给你的随机坐标。这就是你随机分配颜色的原因。好的,我在编辑了q之后对问题进行了更好的解释事实上,我希望u从(0,0,0)坐标增加,但我在中心的值是u=64,而不是0。我解释得更好吗?我无法管理的主要事情是将从(x,y,z)=(0,0,0)开始绘制的正态分布的分散度与中心为最小(u)(u=64)和最大(u)的彩色贴图一起绘制,u=100。@heather by,因此您希望创建一个半径为18、以0为中心的球体。球体由500个点组成。这些点正态分布,中心密度最高。对吗?现在,点的颜色应取决于距中心的距离。在中心,值为64(黑色)在球体的表面上,这个值是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)
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)
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()