我正在尝试生成一个 matplotlib 轮廓图,其中所有值都在白色(包括零)的指定值下,并且所有 nan 值(表示缺失的数据)都是黑色的。我似乎无法让 nan 值与低于/零值的颜色不同。问题的一个简化示例是:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Generate some random data for a contour plot
Z = np.random.rand(10,10)
Z[0:3,0:3] = np.nan # some bad values for set_bad
Z[0:3,7:10] = 0 # some zero values for set_under
x = np.arange(10)
y = np.arange(10)
X,Y = np.meshgrid(x, y)
# Mask the bad data:
Z_masked = np.ma.array(Z,mask=np.isnan(Z))
# Get the colormap and set the under and bad colors
colMap = …Run Code Online (Sandbox Code Playgroud)