Stacked area chart with Python
An area chart, like a line chart, displays the evolution of numeric variables over a continuous period of time. However, in an area chart, the area between the line and x-axis is filled with colour or texture.
More about: Stacked area chart
Basic area chart
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','area'])
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/area_stacked.csv')
df = df.pivot_table(index='months', columns='funding_type', values='funding_million', sort=False)
df = df.reset_index()
df
#compute data for plotting
= df['months']
x = df['Earmarked']
y1 = df['Softly earmarked']
y2 = df['Tightly earmarked']
y3 = df['Unearmarked']
y4
#plot the chart
= plt.subplots()
fig, ax = ['Earmarked', 'Softly earmarked', 'Tightly earmarked', 'Unearmarked'])
ax.stackplot(x, y1, y2, y3, y4, labels
#set chart legend
=(0,1.05), ncol=4)
ax.legend(loc
#set chart title
'Evolution of funding in West Africa region | 2020', pad=50)
ax.set_title(
#set y-axis label
'USD millions')
ax.set_ylabel(
#set y-axis limit
= plt.ylim(0,500)
yl
#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()