简体   繁体   English

plot_surface matplotlib 中的 facecolors

[英]facecolors in plot_surface matplotlib

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.我想重现这个例子: https : //matplotlib.org/3.3.1/gallery/mplot3d/surface3d_3.html#sphx-glr-gallery-mplot3d-surface3d-3-py但颜色不同。

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:当我将 "colortuple =('y', 'b')" 更改为 "colortuple =('#ff7f0e', '#2ca02c') 时,我遇到了这个错误:

 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当它是“colortuple =('gray','orange') 我有这个错误

 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谢谢

Interesting problem.有趣的问题。 This is due to the way the colors array is created in the example.这是由于示例中创建colors数组的方式造成的。

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.因为colors数组是用dtype=str声明的,所以每个单元格只接受 1 个字符,并且您尝试使用的颜色被截断。

From this answer , you can initialize the array with a larger size to circumvent this issue colors = np.empty(X.shape, dtype='U50')这个答案,你可以用更大的尺寸初始化数组来规避这个问题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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM