简体   繁体   English

在matplotlib中设置NaN值的颜色

[英]Set color for NaN values in matplotlib

I'm trying to plot color some array, and convert some of the values to np.nan (for easier interpretation) and expecting different color when plotted (white?), instead it causes problem with the plot and the colorbar. 我正在尝试将颜色绘制成一些数组,并将一些值转换为np.nan(以便于解释)并在绘制时期望不同的颜色(白色?),而不是它会导致绘图和颜色条出现问题。

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

正常的结果

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

条件结果

Any suggestion? 有什么建议吗?

One of the solution is to plot masked array, like here: 其中一个解决方案是绘制掩盖数组,如下所示:

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()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM