首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >plot_surface matplotlib中的facecolors

plot_surface matplotlib中的facecolors
EN

Stack Overflow用户
提问于 2020-10-30 19:17:08
回答 1查看 296关注 0票数 1

我想重现这个例子:https://matplotlib.org/3.3.1/gallery/mplot3d/surface3d_3.html#sphx-glr-gallery-mplot3d-surface3d-3-py,但使用不同的颜色。

原始代码是:

代码语言:javascript
运行
复制
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()

当我将"colortuple =('y','b')“改为"colortuple =('#ff7f0e','#2ca02c')时,我遇到了这个错误:

代码语言:javascript
运行
复制
  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: '#'

)“

当它是"colortuple =(‘灰色’,‘橙色’)时,我有这个错误

代码语言:javascript
运行
复制
  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'

这就像代码只接受基本颜色一样。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-30 21:10:00

有趣的问题。这是由于示例中创建colors数组的方式造成的。

代码语言:javascript
运行
复制
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)]

因为colors数组是用dtype=str声明的,所以它只接受每个单元格1个字符,并且您尝试使用的颜色会被截断。

this answer中,您可以使用更大的大小来初始化数组,以避免此问题colors = np.empty(X.shape, dtype='U50')

代码语言:javascript
运行
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64607336

复制
相关文章

相似问题

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