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 a heatmap is, 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)
  • Matplotlib
  • NumPy
  • Seaborn

To install seaborn, run the pip command as follows:

pip install seaborn

Install Seaborn

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)

That will create a 2-dimensional array with four rows and six 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:

plt.show()

Show Seaborn heatmap

Congratulations! We created our first heatmap!

 

Remove heatmap x tick labels

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

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

Set y axis label

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")

Using xlabel attribute

The result will be as follows:

Show label using xlabel

 

Remove heatmap y tick labels

Seaborn adds the labels for the y-axis by default. To remove them, we can set the yticklabels to false.

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

Set y axis label

 

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')

 

Using ylabel attribute

 

Changing heatmap color

You can change the color of the 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()

Change Seaborn heatmap color

Here cmap equals YlGnBu, which represents the following color:

Heatmap color

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

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

Sequential colormap

You can use the sequential color map 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:

Sequential color map

This image is taken from Matplotlib.org.

Sequential cubehelix palette

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

The cubehelix palette looks like the following:

Cubehelix palette

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

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

The result will be:

Using cubehelix palette

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 the palette on the left side, and 100 is the code for the 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:

Divergent palette

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:

Diverging palette

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:

Add text over 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:

Set heatmap font size

 

Seaborn heatmap colorbar

The colorbar in heatmap looks like the one as below:

Seaborn heatmap colorbar

The attribute cbar of the heatmap is a Boolean attribute; it 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()

Hide colorbar

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()

Set colorbar title

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:

Horizontal colorbar

 

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:

Original ytick labels

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:

Rotated y tick labels

The rotation attribute can be any angle:

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

y tick rotation angel

 

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 flattened 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='')

You should add the fmt attribute when adding annotation other than True and False.

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

Add text and values on heatmap

 

Customize Grid Lines

Customizing the grid lines in Seaborn heatmaps allows for enhanced readability and aesthetic appeal of your data visualization.

Adjusting linewidths and linecolor can distinctly separate data points, making the heatmap more intuitive and visually striking.

We can adjust the width of the lines separating the cells and change their color to make the distinction between cells clearer.

sns.heatmap(df, annot=True, cmap='coolwarm', linewidths=0.5, linecolor='black')
plt.show()

Output:

Customize Grid Lines

The heatmap now has more pronounced grid lines with a width of 0.5 and a black color, making each cell distinctly separated and the data easier to read.

 

Round Annotations Values (Decimal Places)

To format annotations in a Seaborn heatmap with rounded decimal places, you can use the fmt parameter in the heatmap function.

This parameter allows you to specify the string format for the annotations.

For instance, if you want to display numbers rounded to two decimal places, you can use fmt=".2f".

Here’s some example code demonstrating this:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.random.seed(0)
data = pd.DataFrame(np.random.randn(10, 10))
corr = data.corr()
plt.figure(figsize=(10, 8))
sns.heatmap(corr, annot=True, fmt=".2f", cmap='coolwarm')
plt.show()

Output:

Round annotation values

fmt=".2f" ensures that numbers are formatted to two decimal places.

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

Thank you.

14 thoughts on “Seaborn heatmap tutorial (Python Data Visualization)
  1. Hi I was wondering where can I find more information on the keyword “fmt’?
    I tried looking for it in documentation but I didn’t find any

    1. You can find more about any undocumented attribute on the comments in the code of the class itself.
      If you are using PyCharm, you can hold Ctrl key and click on any function and see more info.

  2. Thanks for posting this, highly valuable tutorial. I was looking for such a simple and easy to understand heat map lecture.
    Thanks again bro!!

  3. Excellent tutorial. You are both a good Python programmer and a good teacher.

    Have you done any other tutorials on Python ( or related libraries) in addition to Seaborn ?

    Odesh.

    1. Hi Odesh,

      Thanks for the kind words!
      The Python section contains multiple tutorials about other libraries such as Matplotlib, NumPy, Pandas, OpenCV, Scrapy, PyQt, Kivy, Tkinter, NLTK, TensorFlow, BeautifulSoup, Selenium, Statistics, and much more.

      Regards,

  4. Hi there,

    I am having an error (A rather huge one.)

    Traceback (most recent call last):
    File “”, line 1, in
    import seaborn as sb
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\seaborn\__init__.py”, line 2, in
    from .rcmod import * # noqa: F401,F403
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\seaborn\rcmod.py”, line 7, in
    from . import palettes
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\seaborn\palettes.py”, line 9, in
    from .utils import desaturate, get_color_cycle
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\seaborn\utils.py”, line 14, in
    import matplotlib.pyplot as plt
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\pyplot.py”, line 36, in
    import matplotlib.colorbar
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\colorbar.py”, line 44, in
    import matplotlib.contour as contour
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\contour.py”, line 17, in
    import matplotlib.text as text
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\text.py”, line 16, in
    from .textpath import TextPath # Unused, but imported by others.
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\textpath.py”, line 11, in
    from matplotlib.mathtext import MathTextParser
    File “C:\Users\IamOm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\matplotlib\mathtext.py”, line 27, in
    from PIL import Image
    ModuleNotFoundError: No module named ‘PIL’

    I already have pillow installed.

    Please help.

Leave a Reply

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