Python | Drawing Symmetric Matrix Colormap Plot using Matplotlib

In this tutorial, we are going to learn how to draw a symmetric matrix in python using matplotlib?
Submitted by Anuj Singh, on August 07, 2020

Matplotlib provides an inbuilt function for plotting matrices i.e. matplotlib.pyplot.matshow(). We first define a diagonal matrix using NumPy library function numpy.diag() and then plot it using matplotlib. There are a number of color maps available and for illustration, we are using inferno and cool color maps in this article.

Drawing Symmetric Matrix Colormap Plot  (1)

Drawing Symmetric Matrix Colormap Plot  (2)

Drawing Symmetric Matrix Colormap Plot  (3)

Python code for drawing symmetric matrix colormap plot using matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Example 1
mata = np.random.random([5,4])
mataa = np.matmul(mata, mata.T)
plt.imshow(mataa)
plt.axis(False)
plt.show()

# Example 2
mata = np.random.random([5,4])
mataa = np.matmul(mata, mata.T)
plt.imshow(mataa, cmap='PuBuGn_r')
plt.axis(False)
plt.show()

# Example 3
x = np.arange(5)
y = np.random.randint(1,5,5)
mata = np.random.random([5,4])
mataa = np.matmul(mata, mata.T)
plt.imshow(mataa, cmap='GnBu')
plt.plot(x,y)
plt.axis(False)
plt.show()

Output:

Output is as Figure


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.