How To Plot Heatmap in Python

A Heatmap is a statistical representation that helps to represent the importance of the features in form of colors. In this article, we learn to plot a heatmap in Python. They are different methods to plot heatmap

Method 1: Using Seaborn Library

heatmap() function in seaborn help to plot heatmap

import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
  
df = np.random.rand( 5 , 5)
ax = sns.heatmap( df , linewidth = 0.25 , annot = True)
  
plt.title( "Heat Map" )
plt.show()

Output

How To Plot Heatmap in Python

How To Plot Heatmap in Python

Method 2: Using matplotlib.pyplot library

imshow() function in matplotlib.pyplot help to plot heatmap

import numpy as np
import matplotlib.pyplot as plt
  
data = np.random.random((6 , 6))
plt.imshow( data, interpolation = 'nearest')
  
plt.title( "Heat Map" )
plt.show()

Output

matplotlib.pyplot library

matplotlib.pyplot library

Method 3: Using matplotlib.pyplot library –  pcolormesh() function

The pcolormesh() function in the pyplot module of the matplotlib library is used to create a pseudo-color map with an irregular rectangular grid.

Syntax

matplotlib.pyplot.pcolormesh(*argsalpha=Nonenorm=Nonecmap=Nonevmin=Nonevmax=Noneshading=’flat’antialiased=Falsedata=None**kwargs)

Sample Code

import matplotlib.pyplot as plt
import numpy as np
  
df = np.random.rand(5, 5)
  
plt.pcolormesh(df, cmap = 'autumn')
  
plt.title('Heat Map')
plt.show()

Output

pcolormesh() function

Also, refer

Chess Board Using MatPlotLib Python

Seaborn Multiple Line Plot in Python

Seaborn module of Python

Leave a Reply

Your email address will not be published. Required fields are marked *