我正试图在蒙面数组上使用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()