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

f87krz0w  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(370)

我想让我的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中第一个参数的颜色,在本例中为红色。x1c 0d1x
所有的4个变量{x,y,z,colors}都有相同的(251,251)形状,并且都是numpy数组。https://matplotlib.org/2.0.2/mpl_toolkits/mplot3d/tutorial.html
这个问题类似于facecolors in plot_surface matplotlib,但是我在控制台中没有收到任何错误,只是执行错误。
我做错了什么?

brgchamk

brgchamk1#

facecolors必须是RGB或RGBA,使用2个条目的色彩Map表更简单。

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轴上具有不同细分数的网格,NxNy,看看会发生什么是很有趣的。
如果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()

相关问题