Heatmap with Python
A heatmap is a type of visualization that values are depicted through variations in colour within a two-dimensional matrix of cells. It allows us to visualize complex data and understand it at a glance.
More about: Heatmap
Heatmap
# import libraries
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle', 'heatmap'])
plt.style.use([
#load and reshape data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/correlation/heatmap.csv')
df = df.pivot('location', 'year', 'values')
df
#wrap the long labels
= ['Americas', 'Asia and Pacific', 'East and Horn of Africa and Great Lakes', 'Europe (incl. Turkey)','Middle East/North Africa','Southern Africa', 'West and Central Africa']
y = [ '\n'.join(wrap(l, 20)) for l in y ]
y
#plot the chart
= plt.subplots()
fig, ax = sns.heatmap(df, annot=True, linewidths=.5, yticklabels=y, square=True, cmap=['#DCE9FF','#8EBEFF','#589BE5','#0072BC','#044F85'], cbar=False, fmt=".1f")
ax
#set chart title
'Refugee population by region | 2011-2020')
ax.set_title(
#set subtitle
'Number of people in millions', x=0.345, y=0.87)
plt.suptitle(
#set axis label
'')
ax.set_xlabel('')
ax.set_ylabel(
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -30), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -40), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate(
#adjust chart margin and layout
fig.tight_layout()
#show chart
plt.show()