Python ProgrammingPython Programming

Distinguish NA cells from the other cells in HeatMap

Hatch the background axes patch

import matplotlib.pyplot as plt
import matplotlib.patches as patches

import numpy as np

column_labels = list('ABCDEFGH')
row_labels = list('12345678')

data = np.random.rand(8, 8)
data = np.ma.masked_greater(data, 0.8)

fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.gray, edgecolors='blue', linewidths=1,
                   antialiased=True)

fig.colorbar(heatmap)
ax.patch.set(hatch='..', edgecolor='red')

# Put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0]), minor=False)
ax.set_yticks(np.arange(data.shape[0]), minor=False)

# Want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()

ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)


plt.show()

The following is the output that will be obtained:


Distinguish NA cells from the other cells in HeatMap in Matplotlib