Open In App

Chess Board Using MatPlotLib Python

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads