Home » Python » Seaborn Heatmap Tutorial (Python Data Visualization)

About Mokhtar Ebrahim

Mokhtar Ebrahim
LikeGeeks: Learn more about Linux, Server Administration, Web Development, Python, iOS, and Tech Tips

Seaborn Heatmap Tutorial (Python Data Visualization)

In this tutorial, we will represent data in a heatmap form using a Python library called seaborn. This library is used to visualize data based on Matplotlib.

You will learn what is a heatmap, how to create it, how to change its colors, adjust its font size, and much more, so let’s get started.

What is a heatmap?

The heatmap is a way of representing the data in a 2-dimensional form. The data values are represented as colors in the graph. The goal of the heatmap is to provide a colored visual summary of information.

Create a heatmap

To create a heatmap in Python, we can use the seaborn library. The seaborn library is built on top of Matplotlib. Seaborn library provides a high-level data visualization interface where we can draw our matrix.

For this tutorial, we will use the following Python components:

  • Python 3 (I’ll use Python 3.7)
  • Pandas
  • Matplotlib
  • Numpy
  • Seaborn

To install seaborn, run the pip command as follows:

pip install seaborn
Seaborn Heatmap

Seaborn supports the following plots:

  • Distribution Plots
  • Matrix Plots
  • Regression Plots
  • Time Series Plots
  • Categorical Plots

Okay, let’s create a heatmap now:

Import the following required modules:

import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt

We imported the numpy module to generate an array of random numbers between a given range which will be plotted as a heatmap.

data = np.random.rand(4, 6)

A 2-dimensional array is created with 4 rows and 6 columns. Now let’s store these array values in the heatmap. We can create a heatmap by using the heatmap function of the seaborn module. Then we will pass the data as follows:

heat_map = sb.heatmap(data)

Using matplotlib, we will display the heatmap in the output:

Seaborn Heatmap

Congratulations! Your first heatmap is just created!

Remove heatmap x tick labels

The values in the x axis and y axis for each block in the heatmap are called tick labels. The tick labels are added by default. If we want to remove the tick labels, we can set the xticklabel or ytickelabel attribute of seaborn heatmap to False as below:

heat_map = sb.heatmap(data, xticklabels=False, yticklabels=False)
Seaborn Heatmap

Set heatmap x axis label

We can add a label in x axis by using the xlabel attribute of Matplotlib as shown in the following code:

>>> plt.xlabel("Values on X axis")
Seaborn Heatmap

The result will be as follows:

Seaborn Heatmap

Remove heatmap y tick labels

The labels for y axis are added by default. To remove them, we can set the yticklabels to false.

heat_map = sb.heatmap(data, yticklabels=False)
Seaborn Heatmap

Set heatmap y axis label

You can add the label in y axis by using the ylabel attribute of Matplotlib as shown:

>>> data = np.random.rand(4, 6)
>>> heat_map = sb.heatmap(data)
>>> plt.ylabel('Values on Y axis')
Seaborn Heatmap

Changing heatmap color

You can change the color of seaborn heatmap by using the color map using the cmap attribute of the heatmap.

Consider the code below:

>>> heat_map = sb.heatmap(data, cmap="YlGnBu")
>>> plt.show()
Seaborn Heatmap

Here cmap equals YlGnBu which represents the following color:

Seaborn Heatmap

In Seaborn heatmap, we have three different types of colormaps.

  1. Sequential colormaps
  2. Diverging color palette
  3. Discrete Data

Sequential colormap

The sequential color map is used when the data range from a low value to a high value. The sequential colormap color codes can be used with the heatmap() function or the kdeplot() function.

The sequential color map contains the following colors:

Seaborn Heatmap

This image is taken from Matplotlib.org.

Sequential cubehelix palette

The cubehelix is a form of the sequential color map. The cubehelix is used when there the brightness is increased linearly and when there is a slight difference in hue.

The cubehelix palette looks like the following:

Seaborn Heatmap

You can implement this palette in the code using the cmap attribute:

>>> heat_map = sb.heatmap(data, cmap="cubehelix")

The result will be:

Seaborn Heatmap

Diverging color palette

You can use the diverging color palette when the high and low values are important in the heatmap.

The divergent palette creates a palette between two HUSL colors. It means that the divergent palette contains two different shades in a graph.

You can create the divergent palette in seaborn as follows:

import seaborn as sb
import matplotlib.pyplot as plt
>>> sb.palplot(sb.diverging_palette(200, 100, n=11))
>>> plt.show()

Here 200 is the value for palette on the left side and 100 is the code for palette on the right side. The variable n defines the number of blocks. In our case, it is 11. The palette will be as follows:

Seaborn Heatmap

Discrete Data

In Seaborn, there is a built-in function called mpl_palette which returns discrete color patterns. The mpl_palette method will plot values in a color palette. This palette is a horizontal array.

The diverging palette looks like the following:

Seaborn Heatmap

This output is achieved using the following line of code:

>>> sb.palplot(sb.mpl_palette("Set3", 11))
>>> plt.show()

The argument Set3 is the name of the palette and 11 is the number of discrete colors in the palette. The palplot method of seaborn plots the values in a horizontal array of the given color palette.

Add text over heatmap

To add text over the heatmap, we can use the annot attribute. If annot is set to True, the text will be written on each cell. If the labels for each cell is defined, you can assign the labels to the annot attribute.

Consider the following code:

>>> data = np.random.rand(4, 6)
>>> heat_map = sb.heatmap(data, annot=True)
>>> plt.show()

The result will be as follows:

Seaborn Heatmap

We can customize the annot value as we will see later.

Adjust heatmap font size

We can adjust the font size of the heatmap text by using the font_scale attribute of the seaborn like this:

>>> sb.set(font_scale=2)

Now define and show the heatmap:

>>> heat_map = sb.heatmap(data, annot=True)
>>> plt.show()

The heatmap will look like the following after increasing the size:

Seaborn Heatmap

Seaborn heatmap colorbar

The colorbar in heatmap looks like the one as below:

Seaborn Heatmap

The attribute cbar of heatmap is a Boolean attribute which if set to true tells if it should appear in the plot or not. If the cbar attribute is not defined, the color bar will be displayed in the plot by default. To remove the color bar, set cbar to False:

>>> heat_map = sb.heatmap(data, annot=True, cbar=False)
>>> plt.show()
Seaborn Heatmap

To add a color bar title, we can use the cbar_kws attribute.

The code will look like the following:

>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar'})
>>> plt.show()
Seaborn Heatmap

In the cbar_kws, we have to specify what attribute of the color bar we are referring to. In our example, we are referring to the label (title) of the color bar.

Similarly, we can change the orientation of the color. The default orientation is vertical as in the above example.

To create a horizontal color bar define the orientation attribute of the cbar_kws as follows:

>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar', 'orientation': 'horizontal'})
>>> plt.show()

The resultant color bar will be like the following:

Seaborn Heatmap

Change heatmap colorbar font size

If we need to change the font size of all the components of seaborn, you can use the font_scale attribute of Seaborn.

Let’s set the scale to 1.8 and compare a scale 1 with 1.8:

>>> sb.set(font_scale=1.8)
>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar', 'orientation': 'horizontal'})
>>> plt.show()

This result for scale 1:

Seaborn Heatmap

And the scale of 1.8 will look like this:

Seaborn Heatmap

Change the rotation of tick axis

We can change the tick labels rotation by using the rotation attribute of the required ytick or xtick labels.

First, we define the heatmap like this:

>>> heat_map = sb.heatmap(data)
>>> plt.show()

This is a regular plot with random data as defined in the earlier section.

Notice the original yticklabels in the following image:

Seaborn Heatmap

To rotate them, we will first get the yticklabels of the heatmap and then set the rotation to 0:

>>> heat_map.set_yticklabels(heat_map.get_yticklabels(), rotation=0)

In the set_yticklabels, we passed two arguments. The first one gets the yticklabels of the heatmap and the second one sets the rotation. The result of the above line of code will be as follows:

Seaborn Heatmap

The rotation attribute can be any angle:

>>> heat_map.set_yticklabels(heat_map.get_yticklabels(), rotation=35)
Seaborn Heatmap

Add text and values on the heatmap

In the earlier section, we only added values on the heatmap. In this section, we will add values along with the text on the heatmap.

Consider the following example:

Create random test data:

>>> data = np.random.rand(4, 6)

Now create an array for the text that we will write on the heatmap:

>>> text = np.asarray([['a', 'b', 'c', 'd', 'e', 'f'], ['g', 'h', 'i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p', 'q', 'r'], ['s', 't', 'u', 'v', 'w', 'x']])

Now we have to combine the text with the values and add the result onto heatmap as a label:

>>> labels = (np.asarray(["{0}\n{1:.2f}".format(text,data) for text, data in zip(text.flatten(), data.flatten())])).reshape(4,6)

Okay, so here we passed the data in the text array and in the data array and then flatten both arrays into simpler text and zip them together. The resultant is then reshaped to create another array of the same size which now contains both text and data.

The new array is stored in a variable called labels. The labels variable will be added to heatmap using annot:

>>> heat_map = sb.heatmap(data, annot=labels, fmt='')

The attribute fmt is a string which should be added when adding annotation other than True and False.

On plotting this heatmap, the result will be as follows:

Seaborn Heatmap

Working with seaborn heatmaps is very easy. I hope you find the tutorial useful.

Thank you.

Published on Web Code Geeks with permission by Mokhtar Ebrahim, partner at our WCG program. See the original article here: Seaborn Heatmap Tutorial (Python Data Visualization)

Opinions expressed by Web Code Geeks contributors are their own.

0 0 vote
Article Rating
(+1 rating, 1 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy
Notify of
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x