A CHECKERBOARD IN MATPLOTLIB
Listing 5.12 displays the content of checkerboard1.py that illustrates how to display a checkerboard.
LISTING 5.12: checkerboard1.py
import matplotlib.pyplot as plt from matplotlib import colors import numpy as np data = np.random.rand(10, 10) * 20 # create discrete colormap cmap = colors.ListedColormap(['red', 'blue']) bounds = [0,10,20] norm = colors.BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots() ax.imshow(data, cmap=cmap, norm=norm) # draw gridlines ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2) ax.set_xticks(np.arange(-.5, 10, 1)); ax.set_yticks(np.arange(-.5, 10, 1)); plt.show()
Listing 5.12 defines the NumPy variable data that defines a 2D set of points with ten rows and ten columns. The Pyplot API plot() uses the data variable to display a colored grid-like pattern. Figure 5.9 displays a colored grid whose equations are contained in Listing 5.12.
...