无法在matplotlib中使用RGBA颜色绘制体素

omqzjyyz  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(156)

我尝试使用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()

我知道matplotlib应该允许类似的东西,正如他们自己的文档所述:
面部颜色、边缘颜色:array_like,可选

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.

我知道,在我的例子中,这不起作用(抛出ValueError: Invalid RGBA argument: 0.0),颜色网格的大小是相同的,除了包含rgba值的额外维度。这个应该能用我错过了什么?
注:不是this的重复。不是体素,不是3D。不同的问题。
实际错误转储:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-95-776efc2fdb99> in <module>
     25 fig = plt.figure(figsize=(12, 10), dpi=100)
     26 ax = fig.gca(projection='3d')
---> 27 ax.voxels(voxels, facecolors=colors, edgecolor='k')
     28 plt.show()

~/py3venv/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py in voxels(self, facecolors, edgecolors, shade, lightsource, *args, **kwargs)
   2951             if shade:
   2952                 normals = self._generate_normals(faces)
-> 2953                 facecolor = self._shade_colors(facecolor, normals, lightsource)
   2954                 if edgecolor is not None:
   2955                     edgecolor = self._shade_colors(

~/py3venv/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py in _shade_colors(self, color, normals, lightsource)
   1785             shade[~mask] = 0
   1786 
-> 1787             color = mcolors.to_rgba_array(color)
   1788             # shape of color should be (M, 4) (where M is number of faces)
   1789             # shape of shade should be (M,)

~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba_array(c, alpha)
    292     result = np.empty((len(c), 4), float)
    293     for i, cc in enumerate(c):
--> 294         result[i] = to_rgba(cc, alpha)
    295     return result
    296 

~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
    175         rgba = None
    176     if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 177         rgba = _to_rgba_no_colorcycle(c, alpha)
    178         try:
    179             _colors_full_map.cache[c, alpha] = rgba

~/py3venv/lib/python3.6/site-packages/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
    238         # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
    239         # Test dimensionality to reject single floats.
--> 240         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
    241     # Return a tuple to prevent the cached value from being modified.
    242     c = tuple(c.astype(float))

ValueError: Invalid RGBA argument: 0.0
42fyovps

42fyovps1#

问题是您创建的数组的数据类型为object。这对于字符串来说是可以的,但是RGB数组必须包含数字才能让matplotlib理解它们。因此,在这里删除dtype=object就足够了。

相关问题