3 回答

TA贡献1824条经验 获得超5个赞
对于较新版本的Matplotlib,不再需要使用掩码数组。
例如,让我们生成一个数组,其中每个第7个值都是NaN:
arr = np.arange(100, dtype=float).reshape(10, 10)
arr[~(arr % 7).astype(bool)] = np.nan
我们可以修改当前的颜色图,并使用以下几行来绘制数组:
current_cmap = matplotlib.cm.get_cmap()
current_cmap.set_bad(color='red')
plt.imshow(arr)

TA贡献1817条经验 获得超6个赞
嗯,看来我可以使用蒙版数组来做到这一点:
masked_array = np.ma.array (a, mask=np.isnan(a))
cmap = matplotlib.cm.jet
cmap.set_bad('white',1.)
ax.imshow(masked_array, interpolation='nearest', cmap=cmap)
这应该足够了,尽管我仍然愿意提出建议。:]

TA贡献1804条经验 获得超8个赞
附带一点-我认为这样做会覆盖默认值matplotlib.cm.jet
,因此我通常会制作一个副本:import copy; cmap=copy.copy(matplotlib.cm.jet)
。另外,如果要将0值设置为其他颜色,cmap._init(); cm._lut[:,0] = (1,1,1,1)
则应使用类似的方法。
添加回答
举报