Chess Board Using MatPlotLib Python
Last Updated :
28 Apr, 2025
In this article, we are going to learn how to make a chessboard using Matplotlib in Python programming language.
Prerequisites
- Basic knowledge of Python, NumPy, and Matplotlib.
- NumPy and Matplotlib should be installed in our working requirement.
Installing required libraries
To install NumPy and Matplotlib run the following commands in the command prompt.
pip install numpy
pip install matplotlib
To install Numpy in Linux refer to this article How to Install Numpy on Linux? and for Matplotlib refer to this How to Install Matplotlib on Linux?
NumPy
Numpy is a package in python used for array processing. It offers a multidimensional array object with great speed and tools for working with arrays. This package is used for scientific computing with Python.
Matplotlib
Python's Matplotlib package comes up with a complete tool for building static, animated, and interactive visualizations. It is constructed using NumPy arrays, intended to operate with the larger SciPy stack, and includes a number of graphs, including line, bar, scatter, histograms, and others.
Step-by-Step Implementation of creating a chessboard
Step 1: Importing required modules.
Python3
# Importing Numpy
import numpy as np
# Importing Matplotlib
import matplotlib.pyplot as plt
# Importing LogNorm from matplotlib
from matplotlib.colors import LogNorm
Step 2: Assign the size of the interval dx, dy.
Python3
# Importing required packages
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
# Assign the size of the interval dx, dy.
dx, dy = 0.015, 0.05
Step 3: Create an array P and Q that stores all values from the range -5 to 5 with intervals dx and dy. Then using NumPy inbuilt function arrange() to return the array of the objects.
Python3
# Importing required packages
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
dx, dy = 0.015, 0.05
# Create an array P and Q that store all
# values from range -5 to 5 with interval
# dx and dy.
P = np.arange(-5.0, 5.0, dx)
Q = np.arange(-5.0, 5.0, dy)
Step 4: np.meshgrid() function is used to plot a rectangle grid with vector coordinates, So we use np.meshgrid() function to plot a rectangle grid with vector coordinates.
X, Y = np.meshgrid(x,y)
Step 5: Calculating the alternating position for coloring we are going to use an outer function. this function returns the product of two vectors and modulus the result by 2.
res = np.add.outer(range(8), range(8))%2
Step 6: Use imshow() function to plot.title() function used to set the title of the plot.
plt.imshow(res, cmap="binary_r")
Complete Code:
Python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
dx, dy = 0.016, 0.06
P = np.arange(-5.0, 5.0, dx)
print(P, "\n"*3)
Q = np.arange(-5.0, 5.0, dy)
print(Q, "\n"*3)
P, Q = np.meshgrid(P, Q)
print(P, "\n"*3, Q)
min_max = np.min(P), np.max(P), np.min(Q), np.max(Q)
res = np.add.outer(range(8), range(8)) % 2
plt.imshow(res, cmap="binary_r")
plt.xticks([])
plt.yticks([])
plt.title("Using Matplotlib Python to Create chessboard")
plt.show()
Output:
Similar Reads
Make a violin plot in Python using Matplotlib Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Note: For more inform
3 min read
Violinplot in Python using axes class of Matplotlib Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
How to Plot Mfcc in Python Using Matplotlib? Mel-frequency cepstral coefficients (MFCC) are widely used in audio signal processing and speech recognition tasks. They represent the spectral characteristics of an audio signal and are commonly used as features for various machine-learning applications. In this article, we will explore how to comp
2 min read
Line chart in Matplotlib - Python Matplotlib is a data visualization library in Python. The pyplot, a sublibrary of Matplotlib, is a collection of functions that helps in creating a variety of charts. Line charts are used to represent the relation between two data X and Y on a different axis. In this article, we will learn about lin
6 min read
Multiplots in Python using Matplotlib Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
3 min read