2016-08-06 68 views
4

我想繪製一些數組的顏色,並將一些值轉換爲np.nan(爲了更容易的解釋)並且在繪製時期望不同的顏色(白色?),反而會導致繪圖的問題和彩條。繪圖顏色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')     

conditional result

什麼建議嗎?

回答

4

其中溶液是畫出屏蔽數組,就像這裏:

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

enter image description here