尽管不理想,但屏蔽零值仍然有效。您可以使用来控制它的显示
cmap.set_bad()。
from matplotlib.colors import LinearSegmentedColormapimport matplotlib.pyplot as pltimport numpy as npdic = {'red': ((0., 1, 0), (0.66, 1, 1), (0.89,1, 1), (1, 0.5, 0.5)), 'green': ((0., 1, 0), (0.375,1, 1), (0.64,1, 1), (0.91,0,0), (1, 0, 0)), 'blue': ((0., 1, 1), (0.34, 1, 1), (0.65,0, 0), (1, 0, 0))}a = np.random.rand(10,10)a[0,:2] = 0a[0,2:4] = 0.0001fig, ax = plt.subplots(1,1, figsize=(6,6))cmap = LinearSegmentedColormap('custom_cmap', dic)cmap.set_bad('white')ax.imshow(np.ma.masked_values(a, 0), interpolation='none', cmap=cmap)


