How to draw 2D Heatmap using Matplotlib in python?

A 2-D Heatmap is an information perception instrument that assists with addressing the size of the peculiarity in type of shadings. In python, we can plot 2-D Heatmaps utilizing Matplotlib bundle. There are various strategies to plot 2-D Heatmaps, some of them are examined underneath.

Utilizing Matplotlib, I need to plot a 2D hotness map. My information is a n-by-n Numpy exhibit, each with a worth somewhere in the range of 0 and 1. So for the (I, j) component of this exhibit, I need to plot a square at the (I, j) coordinate in my hotness map, whose tone is relative to the component’s worth in the cluster.

Method 1: Using matplotlib.pyplot.imshow() Function

Syntax:matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None,
vmax=None, origin=None, extent=None, shape=<deprecated parameter>, filternorm=1, filterrad=4.0,
imlim=<deprecated parameter>, resample=None, url=None, \*, data=None, \*\*kwargs)

# Program to plot 2-D Heat map
# using matplotlib.pyplot.imshow() method
import numpy as np
import matplotlib.pyplot as plt
 
data = np.random.random(( 12 , 12 ))
plt.imshow( data , cmap = 'autumn' , interpolation = 'nearest' )
 
plt.title( "2-D Heat Map" )
plt.show()

Output:

eevibes

Method 2: Using Seaborn Library

For this we use seaborn.heatmap() function

Syntaxseaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False,annot=None,
fmt=’.2g’, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, cbar_kws=None, cbar_ax=None,
square=False, xticklabels=’auto’, yticklabels=’auto’, mask=None, ax=None, **kwargs)

# Program to plot 2-D Heat map
# using seaborn.heatmap() method
import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
 
data_set = np.random.rand( 10 , 10 )
ax = sns.heatmap( data_set , linewidth = 0.5 , cmap = 'coolwarm' )
 
plt.title( "2-D Heat Map" )
plt.show()

Output:

eevibes

Method 3: Using matplotlib.pyplot.pcolormesh() Function

Syntax:matplotlib.pyplot.pcolormesh(*args, alpha=None, norm=None, cmap=None, vmin=None, vmax=None,
shading=’flat’, antialiased=False, data=None, **kwargs)

# Program to plot 2-D Heat map
# using matplotlib.pyplot.pcolormesh() method
import matplotlib.pyplot as plt
import numpy as np
 
Z = np.random.rand( 15 , 15 )
 
plt.pcolormesh( Z , cmap = 'summer' )
 
plt.title( '2-D Heat Map' )
plt.show()

Output:

eevibes

Also ReadLearn Arrays in C and C++

Leave a Reply

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