gpt4 book ai didi

python - cmap.set_bad() 对 pcolor() 没有任何影响

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:18 24 4
gpt4 key购买 nike

我正在尝试在掩码数组上使用 pcolor。我想要蒙面元素以一种特殊的颜色出现。我写了一些代码,但没有似乎有效:

import matplotlib as mpl
import matplotlib.pyplot as plt

from numpy import linspace
from numpy.random import randn
from numpy.ma import masked_invalid

D = randn(12*72).reshape((12,72))
D[4,:] = nan
D[6,6] = nan

D = masked_invalid(D)

cmap = mpl.cm.bwr
c map.set_bad('k', 1.)

xbin = linspace(0, 12, 13)
ybin = linspace(-90, 90, 73)

fig = plt.figure()
spl = fig.add_subplot(111)
pl = spl.pcolor(xbin, ybin, D.T, cmap=cmap, edgecolors='none',
vmin=-5, vmax=5)

最佳答案

pcolormesh 的文档说:

Masked array support is implemented via cmap and norm; in contrast, pcolor() simply does not draw quadrilaterals with masked colors or vertices.

所以改用 pcolormesh:

import matplotlib.pyplot as plt
import numpy as np

D = np.random.randn(12*72).reshape((12, 72))
D[4, :] = np.nan
D[6, 6] = np.nan

D = np.ma.masked_invalid(D)

cmap = plt.get_cmap('bwr')
cmap.set_bad(color = 'k', alpha = 1.)

xbin = np.linspace(0, 12, 13)
ybin = np.linspace(-90, 90, 73)

fig = plt.figure()
ax = fig.add_subplot(111)
pl = ax.pcolormesh(xbin, ybin, D.T, cmap = cmap, edgecolors = 'None',
vmin = -5, vmax = 5)
plt.show()

enter image description here

关于python - cmap.set_bad() 对 pcolor() 没有任何影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9214971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com