Stacked column chart with Python
The stacked column chart stacks vertical bars that represent different groups on top of each other. The height of the stacked bar shows the combined value of the groups. They show the cumulative values of a data item and compare parts to the whole.
More about: Stacked column chart
Basic stacked column chart
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle', 'column'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/column_stacked.csv')
df
#reshape df from long to wide
= df.pivot(index='year', columns='rst_type', values='rst_in_thousand')
df = df.reset_index()
df
#compute data array for plotting
= df['year']
x = df['UNHCR resettlement']
y1 = df['Other resettlement']
y2
#plot the chart
= plt.subplots()
fig, ax =ax.bar(x, y1, label='UNHCR resettlement')
rect1=ax.bar(x, y2, bottom=y1, label='Other resettlement')
rect2
#set chart title
'Resettlement by UNHCR and others | 2010-2020', pad=50)
ax.set_title(
#set chart legend
=(0,1.05), ncol=2)
ax.legend(loc
#set y-axis title
'Number of people (thousands)')
ax.set_ylabel(
#set tick label
=True)
ax.tick_params(labelleft
#set x-axis tick and label
ax.set_xticks(x)
#set x-axis limit
= plt.ylim(0, 180)
ylimit
#show grid below the bars
='y')
ax.grid(axis
#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()
Stacked column chart with data label
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','column'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/column_stacked.csv')
df
#reshape df from long to wide
= df.pivot(index='year', columns='rst_type', values='rst_in_thousand')
df = df.reset_index()
df
#prepare data array for plotting
= df['year']
x = df['UNHCR resettlement']
y1 = df['Other resettlement']
y2
#plot the chart
= plt.subplots()
fig, ax =ax.bar(x, y1, label='UNHCR resettlement')
rect1=ax.bar(x, y2, bottom=y1, label='Other resettlement')
rect2
#set chart title
'Resettlement by UNHCR and others | 2010-2020', pad=50)
ax.set_title(
#set subtitle
'Number of people in thousands', x=0.18, y=0.88)
plt.suptitle(
#set chart legend
=(0,1.05), ncol=2)
ax.legend(loc
#set x-axis tick and label
ax.set_xticks(x)
#set formatted data label
='center')
ax.bar_label(rect1, label_type='center')
ax.bar_label(rect2, label_type
#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()