给定的 3D 散点中的 alpha 值列表与绘制的结果不匹配

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

我尝试以 3D 方式可视化点的分类。当我对分散函数中的“c”参数进行分类时,结果符合预期。但是当我对“alpha”参数给出完全相同的分类时,它似乎变成了噪音。

这是一个正弦波的例子。 (我缩放了 alpha 参数以显示它只是噪声)

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['figure.facecolor'] = 'white'

# Orientation sampling
angles = range(0,360,10)
euler = np.array([[[[x,y,z] for z in angles] for y in angles] for x in angles])
euler = euler.reshape((-1,3))

# Make some classification
cls = np.abs(np.sin(euler.mean(1)))
print("min:", cls.min())
print("max:", cls.max())
print("mean:", cls.mean())

# visualize
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(euler[:,0],euler[:,1],euler[:,2], c=cls, s=5, alpha=1.0, cmap='viridis')
plt.show()

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(euler[:,0],euler[:,1],euler[:,2], c='b', s=5, alpha=cls**10/10)
plt.show()

输出:

min: 0.0
max: 0.9999727218865074
mean: 0.6366588835775282

c=cls shows a nice sin wave

alpha=cls**10/10 shows points with random alpha value

我希望在将分类分配给 alpha 值时看到相同的模式。

我目前通过使用点大小来隐藏较低分类点来解决该问题。

matplotlib scatter-plot scatter3d
1个回答
0
投票

目前还不清楚发生了什么。通常,从 matplotlib 3.4 开始,2D 和 3D 散点图都支持 alpha 作为数组。与自动转换为 0 - 1 范围的

c
参数不同,alpha 参数应该已处于正确的范围内。

尽管如此,您可以使用具有 alpha 值的颜色图获得相同的效果。

这是一个例子:

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap, to_rgba
import numpy as np

mpl.rcParams['figure.facecolor'] = 'white'

# Orientation sampling
angles = range(0, 360, 10)
euler = np.array([[[[x, y, z] for z in angles] for y in angles] for x in angles])
euler = euler.reshape((-1, 3))

# Make some classification
cls = np.abs(np.sin(euler.mean(1)))
# visualize
alpha_cmap = LinearSegmentedColormap.from_list('', ['b', to_rgba('b', alpha=0)])

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(euler[:, 0], euler[:, 1], euler[:, 2], c=cls, s=5, cmap=alpha_cmap)
plt.show()

由于此处使用了

c
参数,因此它会自动扩展到 0 到 1 的范围。您可以尝试使用
vmin
vmax
来更改范围。或者您可以在创建时使用不同的较低 alpha
LinearSegmentedColormap
(可以选择颠倒颜色顺序)。

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