为什么matplotlib的plot_surface中的facecolor参数在python中不起作用?

问题描述 投票:0回答:2

我希望我的 python 代码生成具有交替颜色的绘图。为了解决这个问题,交替的颜色是无关紧要的,所以我们假设它们是红色和蓝色。 我正在创建一个交替颜色的 numpy 数组,其形状与输入 x、y 和 z (251, 251)

colortuple = ('red', 'blue')
colors = np.array(
    [np.array([colortuple[int((xn + yn) % len(colortuple))] for xn in range(xrange)]) for yn in range(trange)])

数组将以下内容打印到控制台:

[['red' 'blue' 'red' ... 'red' 'blue' 'red']
 ['blue' 'red' 'blue' ... 'blue' 'red' 'blue']
 ['red' 'blue' 'red' ... 'red' 'blue' 'red']
 ...
 ['red' 'blue' 'red' ... 'red' 'blue' 'red']
 ['blue' 'red' 'blue' ... 'blue' 'red' 'blue']
 ['red' 'blue' 'red' ... 'red' 'blue' 'red']]

这就是我想要的。

但是,当我将它作为facecolors参数传递给plot_surface()时

axes.plot_surface(X=x, Y=y, Z=z, facecolors=colors, linewidth=0)

显示的图仅具有 colortuple 中第一个参数的颜色,在本例中为红色。

所有 4 个变量 {x, y, z, color} 具有相同的 (251, 251) 形状,并且都是 numpy 数组。 我从以下网站激发了我的代码: https://matplotlib.org/2.0.2/mpl_toolkits/mplot3d/tutorial.html

问题类似于 plot_surface matplotlib 中的facecolors 但是我在控制台中没有收到任何错误,只是执行错误。

我做错了什么?

python matplotlib
2个回答
0
投票

facecolors
必须是 RGB 或 RGBA,使用 2 条目颜色图更简单。

from numpy import arange, cos, linspace, meshgrid, sin
from matplotlib import colors, pyplot
N = 32
x = linspace(0, 6.28, N)
X, Y = meshgrid(x, x)
Z = 2*sin(X)+cos(Y/2)
checkers = (arange((N-1)**2)%2).reshape((N-1,N-1))
# if (N-1) is odd, columns are already alternating, otherwise flip odd rows
if N%2: checkers[arange(1, N, 2)] = 1 - checkers[arange(1, N, 2)]
RB = colors.LinearSegmentedColormap.from_list('RB', ['red', 'blue'], N=2)
facecolors = RB(checkers)
fig, ax = pyplot.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, Z, facecolors=facecolors)
pyplot.show()

上面的示例是在方形网格上构建的,如果我们使用在 x 轴和 y 轴上具有不同细分数的网格,

Nx
Ny
,看看会发生什么,这很有趣。

如果

Nx
是奇数,我们需要更改
0
s 和
1
s 网格的尺寸。

       0    1    2    3            The number of cells is one less
  6 +----+----+----+----+          than the number of grid points.
    |    |    |    |    | 5
  5 +----+----+----+----+          Here Nx = 5 (odd) and cNx = 4 (even),
    |    |    |    |    | 4        so in the following code we introduce
  4 +----+----+----+----+          an extra cell to have an odd number
    |    |    |    |    | 3        of cells.
  3 +----+----+----+----+
    |    |    |    |    | 2        Having one extra column is not a problem
  2 +----+----+----+----+          because the last column (in this case)
    |    |    |    |    | 1        is not used by Matplotlib.
  1 +----+----+----+----+
    |    |    |    |    | 0
  0 +----+----+----+----+
    0    1    2    3    4

from numpy import arange, cos, linspace, meshgrid, sin
from matplotlib import cm, colors, pyplot

def f(ax, Nx, Ny, c1, c2):

    x, y = linspace(0, 6.28, Nx), linspace(0, 6.28, Ny)
    X, Y = meshgrid(x, y)
    Z = 2*sin(X)+cos(Y/2)

    ax.set_title('$N_x=%d, N_y=%d.$'%(Nx, Ny))
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    Nx = Nx+1 if Nx%2 else Nx
    c1c2 = colors.LinearSegmentedColormap.from_list('RB', [c1, c2], N=2)
    checkers = c1c2((arange((Nx-1)*(Ny-1))%2).reshape((Ny-1, Nx-1)))
    ax.plot_surface(X, Y, Z, facecolors=checkers, shade=0)
    ax.azim, ax.elev = 80, 60 


fig, ((ax1, ax2),(ax3,ax4)) = pyplot.subplots(
    2, 2, layout='constrained', subplot_kw={"projection": "3d"})

f(ax1, 7, 11, 'red', 'blue'), f(ax2, 7, 12, 'red', 'blue')
f(ax3, 8, 12, 'red', 'blue'), f(ax4, 8, 13, 'red', 'blue')

pyplot.show()

0
投票

问题是,如果您不设置 rcount 和 ccount 参数,渲染器会自动将补丁数量降级到默认最大 50 行/列,并且下采样过程可能会有些不可预测地选择颜色。如果您确实将它们设置为实际的补丁大小,则渲染速度会很慢,但在高分辨率下会很漂亮并且正确。

© www.soinside.com 2019 - 2024. All rights reserved.