【发布时间】:2021-11-02 05:38:40
【问题描述】:
我正在尝试按照以下代码创建一个简单的多维数据集:
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] 时,代码如下:
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) 中删除 dtype 时出现同样的错误。现在我无法调试Invalid RGBA argument,因为我不明白是什么导致了错误。我读了this,但似乎是关于无效形状的错误,而不是无效值。
为什么会发生这个错误?我该如何解决?非常感谢。
【问题讨论】:
标签: python-3.x numpy matplotlib