2016-11-21 60 views
0

說我想區分matplotlib顏色地圖中的NaN。然後:在matlotlib顏色地圖中使用漸變顏色遮罩兩組值顏色編號

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib 

# create a (4,5) matrix with values ranging from 0 to 19 
np_data = np.arange(20).reshape((4,5)).astype(float) 
# add a row with NaNs in the middle 
np_data = np.insert(np_data,2,[np.nan for x in range(0,5)],axis=0) 
# mask invalid data (NaNs) 
np_data = np.ma.masked_invalid(np_data) 

# get figure and ax objects from plot 
fig, ax = plt.subplots() 
# Draw an "X" on transparent values (masked values) 
ax.patch.set(hatch='x', edgecolor='blue') 

# get a predefined color scheme 
reds_cm = plt.get_cmap("Reds") 
# Plot heatmap, add a colorbar and show it 
heatmap = ax.pcolor(np_data, cmap=reds_cm) 
cbar = fig.colorbar(heatmap) 
plt.show() 

圖: heatmap

現在NaN是在情節容易辨認。

現在,我想能夠輕鬆地區分NaNs,0s和其他值。

如果我現在掩蓋了0,我將無法分辨NaN和0。

如何區分顏色映射中的2組值?在這種情況下,一方面是NaN,另一方面是0。

+0

有關問題http://stackoverflow.com/questions/35905393/python-leave-numpy-nan-values-from-matplotlib-heatmap-and-its-legend/35905483#35905483和http://stackoverflow.com/questions/16120481/matplotlib-grayscale-heatmap-with-visually-distinct-na-squares-fields – ImportanceOfBeingErnest

回答

0

我發現this answer來自@unutbu在一個無關的問題。我將他的答案改編成我的問題,並解決了新的艙口也包含在NaN電池中的問題。爲了避免這種情況,在屏蔽numpy數組之前,先獲取值爲0的單元格(我會評論他的回答,以便在上下文中指出這一點,但我沒有所需的聲望)。我只包括從我的問題更改的代碼。

# (previous imports) 
# Import to add patches to "non transparent" cells 
import matplotlib.patches as mpatches 


# (generate np_data) 

# Get mask positions of 0 values before masking NaNs so NaN cells aren't included 
cells_with_0 = np_data == 0 
# mask invalid data (NaNs) 
np_data = np.ma.masked_invalid(np_data) 

# (get color scheme, plot heatmap, plot colorbar) 

#set the background color as gray so the transparent values (NaNs here) use that color 
ax.patch.set_facecolor((0.6, 0.6, 0.6, 1.0)) 
# Draw an "X" on transparent values (masked values) 
ax.patch.set(hatch='x', edgecolor='black') 
# Put an x over cells which have value 0 
for j, i in np.column_stack(np.where(cells_with_0)): 
     ax.add_patch(
      mpatches.Rectangle(
       (i, j),  # (x,y) 
       1,   # width 
       1,   # height 
       fill=False, 
       edgecolor='blue', 
       snap=False, 
       hatch='x' # the more slashes, the denser the hash lines 
     )) 

plt.show() 

新熱圖: enter image description here

+0

我認爲提問者想要一個使用'pcolor'的解決方案。添加很多矩形可能並不總是一個好的解決方案。 – ImportanceOfBeingErnest

2

如果你想告訴你的APPART顏色表的第一個或最後一個值下面的解決方案是一個很好的路要走。您可以修改顏色映射圖,使這些值相當容易變成不同的顏色

reds_cm = plt.get_cmap("name of colormap") 
# init colormap such that its members are available 
reds_cm._init() 
# set the first value to black 
reds_cm._lut[0,: ] = (0,0,0,1) #this is an RGBA tuple 
# set the last value to lightgreen 
reds_cm._lut[-4:,: ] = np.array([149,238,58,255])/255. 

這是一個完整的解決方案。

import numpy as np 
import matplotlib.pyplot as plt 

# create a (4,5) matrix with values ranging from 0 to 19 
np_data = np.arange(20).reshape((4,5)).astype(float) 
# add a row with NaNs in the middle 
np_data = np.insert(np_data,2,[np.nan for x in range(0,5)],axis=0) 
# mask invalid data (NaNs) 
np_data = np.ma.masked_invalid(np_data) 

# get figure and ax objects from plot 
fig, ax = plt.subplots() 
# Draw an "X" on transparent values (masked values) 
ax.patch.set(hatch='x', edgecolor='blue') 

# get a predefined color scheme 
reds_cm = plt.get_cmap("Reds") 
# init colormap such that its members are available 
reds_cm._init() 
# set the first value to black 
reds_cm._lut[0,: ] = (0,0,0,1) 
# set the last value to lightgreen 
reds_cm._lut[-4:,: ] = np.array([149,238,58,255])/255. 

# Plot heatmap, add a colorbar and show it 
heatmap = ax.pcolor(np_data, cmap=reds_cm) 
cbar = fig.colorbar(heatmap) 
plt.show() 

這裏生產 enter image description here