在Matplotlib中为NaN值设置颜色

9

我正在尝试为一些数组上色,并将其中的一些值转换为np.nan(以便更容易解释),并期望在绘制时出现不同的颜色(白色?),但它导致了绘图和色条的问题。

#this is before converted to nan
array = np.random.rand(4,10)
plt.pcolor(array)
plt.colorbar(orientation='horizontal')                

normal result

#conditional value converted to nan
array = np.random.rand(4,10)
array[array<0.5]=np.nan
plt.pcolor(array)
plt.colorbar(orientation='horizontal')                

条件结果

有什么建议吗?

1个回答

11

其中一种解决方案是绘制遮罩数组,例如此处:

import matplotlib.pylab as plt
import numpy as np

#conditional value converted to nan
array = np.random.rand(4,10)
array[array<0.5]=np.nan
m = np.ma.masked_where(np.isnan(array),array)
plt.pcolor(m)
plt.colorbar(orientation='horizontal')       
plt.show()

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接