說我想區分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()
現在NaN是在情節容易辨認。
現在,我想能夠輕鬆地區分NaNs,0s和其他值。
如果我現在掩蓋了0,我將無法分辨NaN和0。
如何區分顏色映射中的2組值?在這種情況下,一方面是NaN,另一方面是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