在 3D 散点图中绘制每点 alpha 值会引发 ValueError

时间:2021-04-20 08:25:50

标签: python matplotlib

我有 3D 数组形式的数据,每个点都有“强度”。根据强度,我想用更高的 alpha 绘制点。有很多低值异常值,因此颜色编码(带有标量浮点数)将不起作用,因为它们会使真实数据黯然失色。

我尝试过的:

#this generates a 3D array with higher values around the center
a = np.array([0,1,2,3,4,5,4,3,2,1])
aa = np.outer(a,a)
aaa = np.einsum("ij,jk,jl",aa,aa,aa)

x_,y_,z_,v_ = [],[],[],[]

from matplotlib.colors import to_rgb,to_rgba

for x in range(aaa.shape[0]):
    for y in range(aaa.shape[1]):
        for z in range(aaa.shape[2]):
            x_.append(x)
            y_.append(y)
            z_.append(z)
            v_.append(aaa[x,y,z])

r,g,b = to_rgb("blue")
color = np.array([[r,g,b,a] for a in v_])

fig = plt.figure()
ax = fig.add_subplot(projection = '3d')
ax.scatter(x_,y_,z_,c =color)

plt.show()

散点图文档说颜色可以是 RGBA 的二维数组,我确实通过了。然而,当我尝试运行代码时,出现以下错误:

<块引用>

ValueError: 'c' 参数有 4000 个元素,与大小为 1000 的 'x' 和 'y' 不一致。

1 个答案:

答案 0 :(得分:0)

我刚刚找到了自己的答案。 “行为 RGB 或 RGBA 的二维数组。”文档中的语句有点令人困惑 - 首先需要将 RGBA 行转换为 RGBA 对象,以便列表理解应为:

color = np.array([to_rgba([r,g,b,a]) for a in v_])