Say I want to distinguish the NaNs in a matplotlib colormap. Then:
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()
Now NaNs are easily identifiable in the plot.
Now, say I want to be able to easily tell apart between NaNs, 0s and the rest of the values.
If I now mask the 0s, I won't be able to tell the NaNs and the 0s apart.
How can I differentiate 2 groups of values in a colormap? In this case NaNs on one hand and 0s in the other.