Reputation: 15
I want to reproduce this example: https://matplotlib.org/3.3.1/gallery/mplot3d/surface3d_3.html#sphx-glr-gallery-mplot3d-surface3d-3-py but with different colors.
the original code is:
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
xlen = len(X)
Y = np.arange(-5, 5, 0.25)
ylen = len(Y)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Create an empty array of strings with the same shape as the meshgrid, and
# populate it with two colors in a checkerboard pattern.
colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
# Plot the surface with face colors taken from the array we made.
surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
# Customize the z axis.
ax.set_zlim(-1, 1)
ax.zaxis.set_major_locator(LinearLocator(6))
plt.show()
when i change "colortuple =('y', 'b')" to "colortuple =('#ff7f0e', '#2ca02c') i am having this error:
File "<ipython-input-41-a355b1dad171>", line 1, in <module>
runfile('C:/Users/camrane/Downloads/dflt_style_changes-1.py', wdir='C:/Users/camrane/Downloads')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/camrane/Downloads/dflt_style_changes-1.py", line 27, in <module>
surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
File "C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1740, in plot_surface
colset = self._shade_colors(colset, normals)
File "C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1790, in _shade_colors
color = mcolors.to_rgba_array(color)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 267, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 168, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 212, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: '#'
)"
and when it is to "colortuple =('gray', 'orange') i have this error
File "<ipython-input-42-a355b1dad171>", line 1, in <module>
runfile('C:/Users/camrane/Downloads/dflt_style_changes-1.py', wdir='C:/Users/camrane/Downloads')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/camrane/Downloads/dflt_style_changes-1.py", line 27, in <module>
surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
File "C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1740, in plot_surface
colset = self._shade_colors(colset, normals)
File "C:\ProgramData\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1790, in _shade_colors
color = mcolors.to_rgba_array(color)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 267, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 168, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\colors.py", line 212, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 'o'
It is like the code accpets only basic colors.
Thanks
Upvotes: 1
Views: 2263
Reputation: 40677
Interesting problem. This is due to the way the colors
array is created in the example.
colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
because the colors
array is declared with dtype=str
, it only accepts only 1 char per cell, and the colors that you are trying to use are truncated.
From this answer, you can initialize the array with a larger size to circumvent this issue colors = np.empty(X.shape, dtype='U50')
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
xlen = len(X)
Y = np.arange(-5, 5, 0.25)
ylen = len(Y)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# Create an empty array of strings with the same shape as the meshgrid, and
# populate it with two colors in a checkerboard pattern.
colortuple = ('xkcd:blue with a hint of purple', '#ff7f0e', 'orange')
colors = np.empty(X.shape, dtype='U50')
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
# Plot the surface with face colors taken from the array we made.
surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
# Customize the z axis.
ax.set_zlim(-1, 1)
ax.zaxis.set_major_locator(LinearLocator(6))
plt.show()
Upvotes: 1