You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 KiB
266 KiB
<html>
<head>
</head>
</html>
Matrix Plots¶
NOTE: Make sure to watch the video lecture, not all datasets are well suited for a heatmap or clustermap.
Imports¶
In [1]:
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.
Source : Estimates for the current year based on data from the World Population Prospects. United Nations.
In [2]:
# 2020 Projections
df = pd.read_csv('country_table.csv')
In [3]:
df
Out[3]:
Heatmap¶
In [4]:
df = df.set_index('Countries')
In [5]:
df
Out[5]:
In [6]:
# Clearly shows life expectancy in different units
sns.heatmap(df)
Out[6]:
In [7]:
rates = df.drop('Life expectancy',axis=1)
In [8]:
sns.heatmap(rates)
Out[8]:
In [9]:
sns.heatmap(rates,linewidth=0.5)
Out[9]:
In [10]:
sns.heatmap(rates,linewidth=0.5,annot=True)
Out[10]:
In [11]:
# Note how its not palette here
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis')
Out[11]:
In [12]:
# Set colorbar based on value from dataset
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis',center=40)
Out[12]:
In [13]:
# Set colorbar based on value from dataset
sns.heatmap(rates,linewidth=0.5,annot=True,cmap='viridis',center=1)
Out[13]:
Clustermap¶
Plot a matrix dataset as a hierarchically-clustered heatmap.
In [14]:
sns.clustermap(rates)
Out[14]:
In [15]:
sns.clustermap(rates,col_cluster=False)
Out[15]:
In [16]:
sns.clustermap(rates,col_cluster=False,figsize=(12,8),cbar_pos=(-0.1, .2, .03, .4))
Out[16]:
In [17]:
rates.index.set_names('',inplace=True)
In [18]:
rates
Out[18]:
In [19]:
# 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))
Out[19]: