A simple solution is to simply cut the patch of background axes. For instance:.
import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm np.random.seed(1977) data = np.random.random((10,25)) data = np.ma.masked_greater(data, 0.8) fig, ax = plt.subplots() im = ax.pcolormesh(data, cmap=cm.gray, edgecolors='white', linewidths=1, antialiased=True) fig.colorbar(im) ax.patch.set(hatch='xx', edgecolor='black') plt.show()

Note that if you do not want borders to be drawn between empty cells, you can use pcolor instead of pcolormesh . For example, if we change the line:
im = ax.pcolormesh(data, cmap=cm.gray, edgecolors='white', linewidths=1, antialiased=True)
in
im = ax.pcolor(data, cmap=cm.gray, edgecolors='white', linewidths=1)
We'll get:

The difference is subtle. Rows are not connected between adjacent empty cells using pcolor . What aesthetics you prefer is purely personal, but it emphasizes the key difference between pcolor and pcolormesh .
source share