Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Heatmap | Plotting with Seaborn
Quizzes & Challenges
Quizzes
Challenges
/
Ultimate Visualization with Python

bookHeatmap

Note
Definition

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value.

Heatmap example

This example uses a heatmap to visualize pairwise correlations between variables.

Creating a Simple Heatmap

seaborn.heatmap() takes a 2D dataset. A common use case is plotting a correlation matrix: given a DataFrame, call .corr() to compute correlations, then pass the resulting matrix to heatmap().

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix) plt.show()
copy

The correlation matrix is created from numeric columns only (numeric_only=True).

Annotation and Colors

Setting annot=True writes the correlation values inside each cell. We can also pick a colormap using cmap.

Note
Note

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore it in the "Choosing color palettes" article).

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix, annot=True, cmap='viridis') plt.show()
copy

The color bar on the right can be removed by setting cbar=False.

Note
Study More

In most of the cases that's all you will need from a heatmap customization, however, you can always explore more in heatmap() documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix, annot=True, cmap='viridis') plt.xticks(rotation=20) plt.yticks(rotation=20) plt.show()
copy
Task

Swipe to start coding

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 3.85

bookHeatmap

Swipe to show menu

Note
Definition

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value.

Heatmap example

This example uses a heatmap to visualize pairwise correlations between variables.

Creating a Simple Heatmap

seaborn.heatmap() takes a 2D dataset. A common use case is plotting a correlation matrix: given a DataFrame, call .corr() to compute correlations, then pass the resulting matrix to heatmap().

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix) plt.show()
copy

The correlation matrix is created from numeric columns only (numeric_only=True).

Annotation and Colors

Setting annot=True writes the correlation values inside each cell. We can also pick a colormap using cmap.

Note
Note

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore it in the "Choosing color palettes" article).

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix, annot=True, cmap='viridis') plt.show()
copy

The color bar on the right can be removed by setting cbar=False.

Note
Study More

In most of the cases that's all you will need from a heatmap customization, however, you can always explore more in heatmap() documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) correlation_matrix = countries_df.corr(numeric_only=True) sns.heatmap(correlation_matrix, annot=True, cmap='viridis') plt.xticks(rotation=20) plt.yticks(rotation=20) plt.show()
copy
Task

Swipe to start coding

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7
single

single

some-alt