【问题标题】:Plotting per-point alpha values in 3D scatterplot throws ValueError在 3D 散点图中绘制每点 alpha 值会引发 ValueError
【发布时间】:2021-04-20 08:25:50
【问题描述】:

我有 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 的 2D 数组,我确实通过了。但是,当我尝试运行代码时,出现以下错误:

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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-03
      • 2017-04-29
      • 2021-04-07
      • 2016-04-28
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多