HeatMaps in Python – How to Create Heatmaps in Python?

Featured Img Heatmap

Hello there! Today we are going to understand the use of heatmaps in Python and how to create them for different datasets.

What are Heatmaps?

Heatmaps visualize the data in 2-D colored maps making use of color variations like hue, saturation, or luminance. Heatmaps describe relationships between variables in form of colors instead of numbers.

These variables are plotted on both axes. The color changes describe the relationship between two values according to the intensity of the color in a particular block. 

Heatmaps have a lot of applications, some of them are listed below:

  1. Visualizing Business Analytics
  2. Exploring Data Analysis
  3. Exploring Marketing and Sales
  4. Visualizing number of visitors on a website or an application

Industries using Heatmap

Many industries make use of heatmaps nowadays. Some of the industries are:

  • Healthcare
  • Finance
  • Technology
  • Real estate

Plotting Heatmaps in Python

There are multiple ways to plot heatmaps in the python programming language. We will be understanding each method one after another. Let’s list out the methods once for your ease.

  1. Using Seaborn Library
  2. Using pcolormesh() function
  3. Using matplotlib.pyplot library

Method 1 : Using Seaborn Library

To plot a heatmap using the seaborn library, we first need to import all the necessary modules/libraries to our program.

Then we generate a ‘random matrix’ of a particular size and then plot the heatmap with the help of heatmap function and pass the dataset to the function.

# 1. Import Modules
import numpy as np
import seaborn as sns
import matplotlib.pylab as plt
plt.style.use("seaborn")

# 2. Generate a 10x10 random integer matrix
data = np.random.rand(10,10)
print("Our dataset is : ",data)

# 3. Plot the heatmap
plt.figure(figsize=(10,10))
heat_map = sns.heatmap( data, linewidth = 1 , annot = True)
plt.title( "HeatMap using Seaborn Method" )
plt.show()
Heatmap Using Seaborn Heatmaps in Python
Heatmap Using Seaborn

Method 2 : Using pcolormesh Function

To plot a heatmap using the pcolormesh function, we first need to import all the necessary modules/libraries to our code.

We will be plotting the heatmap using various cmaps so we will be making use of subplots in matplotlib. The pcolormesh function of matplotlib needs the dataset and we can specify the color map to plot the heatmap.

import matplotlib.pyplot as plt
import numpy as np

data= np.random.rand(10,10)

plt.subplot(2,2,1)
plt.pcolormesh(data, cmap = 'rainbow')
plt.title('HeatMap Using pcolormesh function')

plt.subplot(2,2,2)
plt.pcolormesh(data, cmap = 'twilight')
plt.title('HeatMap Using pcolormesh function')

plt.subplot(2,2,3)
plt.pcolormesh(data, cmap = 'summer')
plt.title('HeatMap Using pcolormesh function')

plt.subplot(2,2,4)
plt.pcolormesh(data, cmap = 'winter')
plt.title('HeatMap Using pcolormesh function')

plt.tight_layout()

plt.show()
Heatmap Using Pcolormesh Function Heatmaps in Python
Heatmap Using Pcolormesh Function

Method 3 : Using matplotlib.pyplot library

To plot a heatmap using matplotlib.pyplot library, we first need to import all the necessary modules/libraries to our program.

Just like the previous method, we will be plotting the heatmap using various cmaps so we will be making use of subplots in matplotlib. The matplotlib library makes use of the imshow function which needs the dataset and we can specify the color map to plot the heatmap.

import numpy as np
import matplotlib.pyplot as plt

data= np.random.random((10,10))

plt.subplot(2,2,1)
plt.imshow( data, interpolation = 'nearest',cmap="rainbow")
plt.title('HeatMap Using Matplotlib Library')

plt.subplot(2,2,2)
plt.imshow( data, interpolation = 'nearest',cmap="twilight")
plt.title('HeatMap Using Matplotlib Library')

plt.subplot(2,2,3)
plt.imshow( data, interpolation = 'nearest',cmap="summer")
plt.title('HeatMap Using Matplotlib Library')

plt.subplot(2,2,4)
plt.imshow( data, interpolation = 'nearest',cmap="ocean")
plt.title('HeatMap Using Matplotlib Library')

plt.tight_layout()

plt.show()
Heatmap Using Matplotlib Library
Heatmap Using Matplotlib Library

Ending words

Thank you for reading this tutorial! I believe I have covered all the methods to plot heatmaps and now you can try plotting them for real-time data! Stay tuned for more such tutorials!