🚀
Seaborn
06 Matrix Plots
++++
Data Science
May 2026×Notebook lesson

Notebook converted from Jupyter for blog publishing.

06-Matrix-Plots

Driptanil Datta
Driptanil DattaSoftware Developer

Matrix Plots

NOTE: Make sure to watch the video lecture, not all datasets are well suited for a heatmap or clustermap.

Imports

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

The Data

World Population Prospects publishes United Nations population estimates for all world countries and every year from 1950 to 2020, as well as projections for different scenarios (low, middle and high variants) from 2020 to 2100. The figures presented here correspond to middle variant projections for the given year.

https://www.ined.fr/en/everything_about_population/data/all-countries/?lst_continent=900&lst_pays=926 (opens in a new tab)

Source : Estimates for the current year based on data from the World Population Prospects. United Nations.

# 2020 Projections
df = pd.read_csv('country_table.csv')
df
HTML
MORE
Countries
Birth rate
Mortality rate
Life expectancy
Infant mortality rate

Heatmap

df = df.set_index('Countries')
df
HTML
MORE
Birth rate
Mortality rate
Life expectancy
Infant mortality rate
Growth rate
# Clearly shows life expectancy in different units
sns.heatmap(df)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 1
rates = df.drop('Life expectancy',axis=1)
sns.heatmap(rates)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 2
sns.heatmap(rates,linewidth=0.5)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 3
sns.heatmap(rates,linewidth=0.5,annot=True)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 4
# Note how its not palette here
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis')
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 5
# Set colorbar based on value from dataset
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis',center=40)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 6
# Set colorbar based on value from dataset
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis',center=1)
RESULT
<AxesSubplot:ylabel='Countries'>
PLOT
Output 7

Clustermap

Plot a matrix dataset as a hierarchically-clustered heatmap.

sns.clustermap(rates)
RESULT
<seaborn.matrix.ClusterGrid at 0x158e27976c8>
PLOT
Output 8
sns.clustermap(rates,col_cluster=False)
RESULT
<seaborn.matrix.ClusterGrid at 0x158e235c9c8>
PLOT
Output 9
sns.clustermap(rates,col_cluster=False,figsize=(12,8),cbar_pos=(-0.1, .2, .03, .4))
RESULT
<seaborn.matrix.ClusterGrid at 0x158e2ffc848>
PLOT
Output 10
rates.index.set_names('',inplace=True)
rates
HTML
MORE
Birth rate
Mortality rate
Infant mortality rate
Growth rate
AFRICA
# Recall you can always edit the DF before seaborn
sns.clustermap(rates,col_cluster=False,figsize=(12,8),cbar_pos=(-0.1, .2, .03, .4))
RESULT
<seaborn.matrix.ClusterGrid at 0x158e354b508>
PLOT
Output 11


Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.