Seaborn Heatmap Tutorial – A Comprehensive Guide

Filed Under: Python
Seaborn Heatmaps

Hey, folks! In this article, we will be discussing about Data Visualization through Seaborn Heatmaps.

Understanding Heatmap in Seaborn library

Python has got various modules to prepare and present the data in a visualized form for a better understanding of the built data model.

Python Seaborn module is used to visualize the data and explore various aspects of the data in a graphical format. It is built on top of the Python Matplotlib module which too serves functions to plot the data in a varied manner.

Seaborn cannot be considered as an alternative to Matplotlib, but indeed can be considered as a helping feature in data exploration and visualization.

Seaborn has multiple built-in functions to build graphs for data visualization. One of the important functions in the direction of Data exploration and visualization is HeatMaps.

Seaborn Heatmaps represent the data in the form of a 2-dimensional format. Heatmaps visualize the data and represent in the form of a summary through the graph/colored maps.

It uses various color palettes and different parameters to add more visualization features to the graph and thus adds to exploration of data effectively.

In order to get started with Heatmaps, we need to install the Seaborn module using the below syntax–

Syntax:

pip install seaborn

Seaborn requires the following modules to be installed in a prior manner:


1. Creating a HeatMap

Let’s create a basic Heatmap with the following syntax to create a visualization graph of the data provided to it.

Syntax:

seaborn.heatmap(data)

Example:

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot)
plt.show()

In the above snippet of code, we have used numpy.random.rand(m,n) function to randomly generate some data with 6 rows and 5 columns to be fed to the heatmap. Further, pyplot.show() function is used to represent the heatmap with proper formatting.

Output:

Creation of a HeatMap
Creation of a HeatMap

2. Remove labels in the HeatMap

As seen in the above Heatmap representation, the values/data points represented by x-axis and y-axis is known as tick labels. They represent the scale of the data plotted and visualized using the Heatmaps.

The tick labels are of the following types-

  • x-tick labels
  • y-tick labels

Removing y-label from a HeatMap

By default, the tick labels are present in the Heatmaps. In order to remove the y-tick, we can use the below syntax:

seaborn.heatmap(data,yticklabels=False)

Example:

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot,yticklabels=False)
plt.show()

Output:

Removal of y-label
Removal of y-label

Removing x-label from a HeatMap

To remove the x-tick label scale, use the below syntax:

seaborn.heatmap(data,xticklabels=False)

Example:

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot,xticklabels=False)
plt.show()

Output:

Removal of x-label
Removal of x-label

3. Setting labels in HeatMap

For adding better value and understanding to the Heatmap, it is possible to add labels that would contribute to adding more meaning in understanding the visualized data.


1. Set x-label

The following syntax can be used to add a text label to the x-tick axis using matplotlib in-built function:

pyplot.xlabel("label")

Example:

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot)
plt.xlabel("Numbers")
plt.show()

Output:

Setting x label
Setting x label

2. Set y-label

In a similar manner, the following syntax can be inculcated to add a text label to the y-tick axis:

pyplot.ylabel("label")

Example:

import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot)
plt.ylabel("Range")
plt.show()

Output:

Setting y-label
Setting y-label

4. Adding text values to the HeatMap

We can add the values represented by the 2-dimensional format of Heatmap that would add value to the better understanding of the represented data using the below syntax:

seaborn.heatmap(data,annot=True)

The annot parameter is set to True, to display the data plotted by the heatmap.

Example 1: Adding text values to the randomly generated data using Heatmaps

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

data_plot = np.random.rand(6,5)

map = sn.heatmap(data_plot,annot=True)

plt.xlabel("Numbers")
plt.ylabel("Range")
plt.show()

Output:

Adding text to the HeatMap
Adding text to the HeatMap

Example 2: Adding the data values from the dataset to represent in the Heatmap

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
map = sn.heatmap(data_set,annot=True)
plt.show()

Input Dataset:

Input Dataset For HeatMap
Input Dataset For HeatMap

Output:

Adding Text To The HeatMap Of A Dataset
Adding Text To The HeatMap Of A Dataset

5. ColorMaps in Seaborn HeatMaps

The Colormap helps understand the data presented by the Heatmaps effectively. Colormaps represent the distribution of the data wherein we can analyze the data in terms of the minimum and maximum values represented by the colors from the colorbar.


1. Sequential colormaps

Sequential colormaps are used when the data experiences gradual and linear rise in the values of the data/population. Thus, sequential colormaps can be used to represent the linear rise from low to high values, respectively.

We can implement the sequential colormap by setting the cmap attribute to ‘cubehelix

Syntax:

seaborn.heatmap(data,cmap='cubehelix')

Example:

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])

map = sn.heatmap(data_set,annot=True,cmap="cubehelix")
plt.show()

Output:

Sequential Colormap
Sequential Colormap

2. Diverging color palette

Diverging color palette creates a colormap as a combination of divergence between two colors.

Syntax:

cmap = seaborn.diverging_palette(h_neg and h_pos, sep=value, l=value, as_cmap=True)
  • h_neg and h_pos: The values for negative and positive extends of the map. Ranges between 0-359.
  • l: It is used to add lightness to both the extents of the map. Ranges between 0-100.
  • sep: The sep parameter represents the size of the intermediate region of data in the heatmap.
  • as_cmap: A boolean parameter, when set to True, it represents a matplotlib colormap object.

Example:

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
cmap = sn.diverging_palette(320, 40, sep=40, as_cmap=True)
map = sn.heatmap(data_set,annot=True,cmap=cmap)
plt.show()

Output:

Diverging Palette Colormap
Diverging Palette Colormap

3. Light and Dark palette colorMap

Using seaborn heatmaps, we can obtain a colormap with a blend of either light or dark values to visualize the data in a better manner.

Types of blending colormap:

  • Light palette colormap: It blends the given color from light to dark, representing the data from low to high values of the population.
  • Dark palette colormap: It blends the given color from dark to light, representing the data from low to high values.

Syntax: Light palette

cmap = seaborn.light_palette("color-code",as_cmap=True)

Example 1: Light palette

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
cmap = sn.light_palette("#3fdd01", as_cmap=True)
map = sn.heatmap(data_set,annot=True,cmap=cmap)
plt.show()

Output:

Light Palette Colormap
Light Palette Colormap

Syntax: Dark palette

seaborn.dark_palette("color-code",as_cmap=True)

Example 2: Dark palette

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
cmap = sn.dark_palette("#3fdd01", as_cmap=True)
map = sn.heatmap(data_set,annot=True,cmap=cmap)
plt.show()

Output:

Dark Palette Colormap
Dark Palette Colormap

4. Discrete ColorMap

If the dataset/population contains discrete data values, we can use the seaborn.mpl_palette() function to represent the discrete values with discrete colors.

Syntax:

seaborn.mpl_palette("Set3",value)
  • Set3: It is the name of the color palette (play around with other colormaps here)
  • value: Number of discrete colors to be presented in a palette.

Example:

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
cmap = sn.mpl_palette("Set3", 20)
map = sn.heatmap(data_set,annot=True,cmap=cmap)
plt.show()

Output:

Discrete ColorMap
Discrete ColorMap

6. Seaborn HeatMap colorbar

The Colorbar gives information about the color represented by the visualized data and also represents the range of values that depicts the data plotted by the Heatmaps.

By default, a colorbar is present in the Heatmap. If we wish to remove the colorbar from the heatmap, the below syntax can help you out with it:

seaborn.heatmap(data,cbar=False)

Example 1:

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

data_plot = np.random.rand(6,5)
map = sn.heatmap(data_plot,annot=True,cbar=False)

plt.xlabel("Numbers")
plt.ylabel("Range")
plt.show()

Output:

Disable Colorbar
Disable Colorbar

We can customize the Heatmap by providing the range to the scale of values represented by the colors of the colorbar using the below syntax:

seaborn.heatmap(data,cmap,vmin=value,vmax=value)

Example 2:

import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt

data = pd.read_csv("C:/Python_edwisor/mtcars.csv")
data_set = pd.DataFrame(data.iloc[1:5,1:5])
cmap = sn.mpl_palette("Set3", 5)
map = sn.heatmap(data_set,annot=True,cmap=cmap,vmin=10, vmax=20)

plt.show()

In the above example, we have set the scale of the colorbar from 10-20.

Output:

Changing the scale of Colorbar
Changing the scale of Colorbar

Conclusion

Thus, in this article, we have understood the functioning of Seaborn Heatmaps.

I strongly recommend you to go through Python Matplotlib module for deep understanding of Data Visualization.


References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages