首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:无效的RGBA参数。为什么会这样呢?我怎么才能修复它?

ValueError:无效的RGBA参数。为什么会这样呢?我怎么才能修复它?
EN

Stack Overflow用户
提问于 2021-09-04 04:17:27
回答 1查看 204关注 0票数 2

我试着按照下面的代码创建一个简单的多维数据集:

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


# Create axis
axes = [5,5,5]

# Create Data
data = np.ones(axes, dtype=np.bool)

# Controll Tranperency
alpha = 0.9

# Control colour RGBA colour
colors = np.empty(axes + [4], dtype=np.float32)

colors[0] = [1, 0, 0, alpha] # red
colors[1] = [0, 1, 0, alpha] # green
colors[2] = [0, 0, 1, alpha] # blue
colors[3] = [1, 1, 0, alpha] # yellow
colors[4] = [1, 1, 1, alpha] # grey

# Plot figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Voxels are used for customizations of sizes, positions, and colors.
ax.voxels(data, facecolors=colors, edgecolors='grey')
plt.show()

它工作得很好。但当我更改axes = [10, 10, 10]时,代码如下:

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


# Create axis
axes = [10, 10, 10]

# Create Data
data = np.ones(axes, dtype=np.bool)

# Controll Tranperency
alpha = 0.9

# Control colour RGBA colour
colors = np.empty(axes + [4], dtype=np.float32)

colors[0] = [1, 0, 0, alpha] # red
colors[1] = [0, 1, 0, alpha] # green
colors[2] = [0, 0, 1, alpha] # blue
colors[3] = [1, 1, 0, alpha] # yellow
colors[4] = [1, 1, 1, alpha] # grey

# Plot figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Voxels are used for customizations of sizes, positions, and colors.
ax.voxels(data, facecolors=colors, edgecolors='grey')
plt.show()

它有时工作,有时不工作,并抛出错误:ValueError: Invalid RGBA argument: 4.435719e+27。当我在data = np.ones(axes, type=np.bool)中删除数据类型时,也会出现同样的错误。现在我无法调试Invalid RGBA argument,因为我不知道是什么导致了这个错误。我读了this,但似乎是关于无效形状的错误,而不是无效值的错误。

为什么会发生这种错误?我怎么才能修复它?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-04 04:37:27

之所以会出现这个错误,是因为np.empty创建的基本上是随机填充的数组(有时会使用空的内存空间,这就是为什么它有时会为您工作)。这对于axes = [5, 5, 5]来说不是问题,因为当你分配颜色时,你填充了适当的RGBA值,但是对于更大的轴,它也不会起作用。

查看当axes为[5, 5, 5]时打印colors的结果与使用[10, 10, 10]时不起作用的次数

修复方法:使用np.zeros而不是np.empty来确保缺少值时得到的是零:

代码语言:javascript
运行
复制
axes = [10, 10, 10]
colors = np.zeros(axes + [4], dtype=np.float32)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69052375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档