Seaborn Heatmap Tutorial (Python Data Visualization)

Seaborn Heatmap Tutorial (Python Data Visualization)

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 supports the following plots:

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

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:

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)

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

The result will be as follows:

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)

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


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

Here cmap equals YlGnBu which represents the following color:

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:

This image is taken from

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:

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

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

The result will be:

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

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:

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:

This output is achieved using the following line of code:

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

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)

The result will be as follows:

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)

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


Seaborn heatmap colorbar

The colorbar in heatmap looks like the one as below:

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)

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

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

The resultant color bar will be like the following:


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

This result for scale 1:

And the scale of 1.8 will look like this:


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)

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

Notice the original yticklabels in the following image:

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:

The rotation attribute can be any angle:

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


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.

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:

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

 Recommended Reading

Is your Django app slow? Think like a data scientist, not an engineer

Python project: 3D chess in augmented reality

5 Common Python Mistakes and How to Fix Them

Build & Deploy A Python Web App | Flask, Postgres & Heroku

Snake Game with python curses (new features)

Django 2 Ajax CRUD with Python 3.7 and jQuery

How to Program a GUI Application (with Python Tkinter)

Thank you.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language