Issue
I'm trying to create a simple cube following this code:
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()
It works well. But when I change axes = [10, 10, 10]
, here is the code:
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()
sometimes it works, sometimes it doesn't, and throw the error: ValueError: Invalid RGBA argument: 4.435719e+27
. The same error when I remove dtype in data = np.ones(axes, type=np.bool)
. Now I am unable to debug the Invalid RGBA argument
because I don't understand what is causing the error. I read this, but it seems that error about invalid shape, not invalid value.
Why can this error happen? How can I fix it? Thank you very much.
Solution
You're getting this error because np.empty
created basically randomly filled arrays (sometimes uses empty memory space, which is why it sometimes will work for you). This isn't a problem with axes = [5, 5, 5]
because you're filling out proper RGBA values when you assign colors, but with bigger axes it wont work as well.
Look at the result of printing colors
when axes is [5, 5, 5]
vs. the times it doesn't work for you with [10, 10, 10]
To fix: use np.zeros
instead of np.empty
to make sure zeros is what you get for missing values:
axes = [10, 10, 10]
colors = np.zeros(axes + [4], dtype=np.float32)
Answered By - Ofer Sadan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.