N = 15# make an empty data setdata = np.ones((N, N)) * np.nan# fill in some fake datafor j in range(3)[::-1]: data[N//2 - j : N//2 + j +1, N//2 - j : N//2 + j +1] = j# make a figure + axesfig, ax = plt.subplots(1, 1, tight_layout=True)# make color mapmy_cmap = matplotlib.colors.ListedColormap(['r', 'g', 'b'])# set the 'bad' values (nan) to be white and transparentmy_cmap.set_bad(color='w', alpha=0)# draw the gridfor x in range(N + 1): ax.axhline(x, lw=2, color='k', zorder=5) ax.axvline(x, lw=2, color='k', zorder=5)# draw the boxesax.imshow(data, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N], zorder=0)# turn off the axis labelsax.axis('off')