Streamgraph with Python
A streamgraph is a variation of a stacked area chart. Instead of displaying values on top of a fixed, straight baseline at the bottom of the stack, the values of the streamgraph are displaced around a central baseline.
More about: Streamgraph
Streamgraph
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','streamgraph'])
plt.style.use([
#load and reshape the data
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/streamgraph.csv')
df = df.pivot_table(index='year', columns='population_type', values='population_number')
df = df.reset_index()
df = df.fillna(0)
df
#compute data for plotting
= df['year']
x = df['VDA']
y1 = df['OOC']
y2 = df['STA']
y3 = df['IDP']
y4 = df['ASY']
y5 = df['REF']
y6
#plot the chart
= plt.subplots()
fig, ax = ['#EF4A60', '#999999', '#E1CC0D', '#00B398', '#18375F', '#0072BC'], labels = [ 'Venezuelans displaced abroad', 'Others of concern', 'Stateless persons', 'IDPs', 'Asylum-seekers', 'Refugees' ], baseline='weighted_wiggle')
ax.stackplot(x, y1, y2, y3, y4, y5, y6, colors
#set chart title
'Evolution of people of concern to UNHCR | 1991-2020', pad=50)
ax.set_title(
#set chart legend
=(0,1), ncol=3)
ax.legend(loc
#set y-axis label
'Number of people (millions)')
ax.set_ylabel(
#format y-axis tick labels
def number_formatter(x, pos):
if x >= 1e6:
= '{:1.0f}M'.format(x*1e-6)
s elif x < 1e6 and x > 0:
= '{:1.0f}K'.format(x*1e-3)
s elif x > -1e6 and x < 0:
= '{:1.0f}K'.format(x*1e-3)
s elif x <= -1e6:
= '{:1.0f}M'.format(x*1e-6)
s else:
= '{:1.0f}'.format(x)
s return s
ax.yaxis.set_major_formatter(number_formatter)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -25), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -35), 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()