This Python program contains a simple program to generate a Chessboard using the Matplotlib Python library. Matplotlib Python has an extensive library for creating stable, animated, and interactive data visualizations.
This python program is self-explanatory, and comments are written in it for better understanding.
Example:
An example of a Python Matplotlib Chessboard:
#Importing modules (numpy, and matplotlib.pyplot)
import numpy as np
import matplotlib.pyplot as pyplot
#Declare the size of the interval dx, dy.
(dx, dy) = (0.015, 0.015)
#Create an array x and y that stores all values with dx and dy intervals,
#respectively, from -4 to 4 (since we need the square). arange() is a NumPy library function
#that gives an array of objects with equally spaced values within a defined interval.
x = np.arange(-4.0, 4.0, dx)
y = np.arange(-4.0, 4.0, dy)
#Plot a rectangle grid with vector coordinates.
(X, Y) = np.meshgrid(x, y)
extent = (np.min(x), np.max(x), np.min(y), np.max(y))
#To calculate the alternate position for coloring, use the outer function,
#which results in two vectors, and the modulus is 2.
z1 = np.add.outer(range(8), range(8)) % 2
#imshow function in MatPlotLib helps in plotting
pyplot.imshow(z1, cmap='binary_r', interpolation='nearest', extent=extent, alpha=1)
def chess(x, y):
return (1 - x / 2 + x ** 5 + y ** 6) * np.exp(-(x ** 2 + y ** 2))
z2 = chess(X, Y)
pyplot.imshow(z2, alpha=0, interpolation='bilinear', extent=extent)
#set the plot's title.
pyplot.title('Chess Board in Python')
# Save the chart file
#pyplot.savefig('matplotlib_pie_chart01.png', dpi=300)
# Print the chart
pyplot.show()
Program Output: