Python 无法在matplotlib中使用每个体素的RGBA颜色打印体素

Python 无法在matplotlib中使用每个体素的RGBA颜色打印体素,python,matplotlib,Python,Matplotlib,我尝试使用matplotlib的体素示例,但无法指定RGBA颜色 代码如下: import matplotlib.pyplot as plt; import numpy as np # using matplotlib 3.1.1 from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import x, y, z = np.indices((12, 12, 12)) # draw cuboids in the top l

我尝试使用matplotlib的体素示例,但无法指定RGBA颜色

代码如下:

import matplotlib.pyplot as plt; import numpy as np  # using matplotlib 3.1.1
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import


x, y, z = np.indices((12, 12, 12))

# draw cuboids in the top left and bottom right corners, and a link between them
cube1 = (x < 3) & (y < 3) & (z < 3)
cube2 = (x >= 9) & (y >= 9) & (z >= 9)
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2

# combine the objects into a single boolean array
voxels = cube1 | cube2 | link

# From the example - this works
colors = np.empty(voxels.shape, dtype=object)
colors[link] = 'red'
colors[cube1] = 'blue'
colors[cube2] = 'green'

# The following sets the values correctly, but fails if used as the colors param
# colors = np.empty(voxels.shape + (3,), dtype=object)
# colors[link, :] = ((1., 0., 0.))
# colors[cube1, :] = ((0., 1., 0.))
# colors[cube2, :] = ((0., 0., 1.))

fig = plt.figure(figsize=(12, 10), dpi=100)
ax = fig.gca(projection='3d')
ax.voxels(voxels, facecolors=colors, edgecolor='k')
plt.show()
我知道,在我的情况下,这不起作用抛出ValueError:Invalid RGBA参数:0.0,颜色网格的大小是相同的,除了包含RGBA值的额外维度。这应该行得通。我错过了什么

注:不可复制。不是体素,不是3D。不同的问题

实际错误转储:

-------------------------------------- ValueError回溯最近一次呼叫last 在里面 25 fig=plt.figurefigsize=12,10,dpi=100 26 ax=图GCA投影='3d' -->27 ax.voxelsvoxels,facecolors=colors,edgecolor='k' 28 plt.show ~/py3venv/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py,在体素self、facecolors、edgecolors、shade、lightsource、*args、**kwargs中 2951如果阴影: 2952法线=自身。\u生成\u法线面 ->2953 facecolor=self.\u shade\u colorsfacecolor,法线,光源 2954如果edgecolor不是无: 2955 edgecolor=自身颜色 ~/py3venv/lib/python3.6/site-packages/mpl_工具箱/mplot3d/axes3d.py in_shade_colorsself、color、normals、lightsource 1785阴影[~遮罩]=0 1786 ->1787颜色=mcolors.to_rgba_arraycolor 1788颜色的形状应为M,其中M为面数 1789阴影的形状应为M, ~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba_arrayc,alpha 292结果=np.emptylenc,4,浮点 293对于i,C中的cc: ->294结果[i]=至_rgbacc,α 295返回结果 296 ~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in to_rgbac,alpha 175 rgba=无 176如果rgba为无:抑制缓存查找失败的异常链接。 ->177 rgba=_至_rgba_无颜色循环,α 178试试: 179 _colors_full_map.cache[c,alpha]=rgba ~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in_to_rgba_no_colorcyclec,alpha 238 float`和`np.array..astypefloat`都将0.5转换为0.5。 239测试维度以拒绝单个浮动。 ->240 raise VALUERRORRRGBA参数无效:{!r}.formatorig\u c 241返回元组以防止修改缓存的值。 242 c=tuplec.astypefloat ValueError:无效的RGBA参数:0.0
问题是您使用datatype对象创建了数组。这对于字符串来说很好,但RGB数组必须包含数字才能让matplotlib理解它们。因此,在这里删除dtype=object就足够了。

刚刚添加了版本号-3.1.1刚刚添加了它。这很奇怪-看起来matplotlib在这种情况下只需要一个3D的东西,而我给它一个4D的东西会让它非常混乱。好的,对不起。您现有的代码可以正常工作,但注释后的代码无法正常工作。这是因为您使用datatype对象创建了数组。但RGB阵列必须包含数字。狗娘养的,你说得对。你能回答这个问题吗?
The color to draw the faces and edges of the voxels. Can only be passed as keyword arguments. This parameter can be:

    ...

    * A 4D ndarray of rgb/rgba data, with the components along the last axis.