Grouped column chart with Python
Grouped column charts are a type of colour-coded column chart used to represent and compare different categories of two or more groups.
More about: Grouped column chart
Basic grouped column chart
# import libraries
import numpy as np
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_grouped.csv')
df
#reshape df from long to wide
= df.pivot(index='year', columns='main_office', values='refugee_number')
df = df.reset_index()
df
#prepare data array for plotting
= df['year']
labels = df['East and Horn of Africa and Great Lakes']
category1 = df['Southern Africa']
category2 = df['West and Central Africa']
category3
#set x-axis ticks label location
= np.arange(len(labels))
x
#set bar width
= 0.28
width
#plot the chart
= plt.subplots()
fig, ax = ax.bar(x - width, category1, width, edgecolor='white', label='East and Horn of Africa and Great Lakes')
rects1 = ax.bar(x, category2, width, edgecolor='white', label='Southern Africa')
rects2 = ax.bar(x + width, category3, width, edgecolor='white', label='West and Central Africa')
rects3
#set chart title
'Refugee in Africa region | 2018-2021', pad=50)
ax.set_title(
#set chart legend
=(0,1.04), ncol=2)
ax.legend(loc
#set y-axis title
'Number of people (millions)')
ax.set_ylabel(
#set y-axis label
=True)
ax.tick_params(labelleft
#set x-axis tick and label
ax.set_xticks(x, labels)
#show grid below the bars
='y')
ax.grid(axis
#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 >= 1e3:
= '{:1.0f}K'.format(x*1e-3)
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()
Grouped column chart with data label
# import libraries
import numpy as np
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_grouped.csv')
df
#reshape df from long to wide
= df.pivot(index='year', columns='main_office', values='refugee_number')
df = df.reset_index()
df
#compute data array for plotting
= df['year']
labels = df['East and Horn of Africa and Great Lakes']
category1 = df['Southern Africa']
category2 = df['West and Central Africa']
category3
#set x-axis ticks label location
= np.arange(len(labels))
x
#set bar width
= 0.28
width
#plot the chart
= plt.subplots()
fig, ax = ax.bar(x - width, category1, width, edgecolor='white', label='East and Horn of Africa and Great Lakes')
rects1 = ax.bar(x, category2, width, edgecolor='white', label='Southern Africa')
rects2 = ax.bar(x + width, category3, width, edgecolor='white', label='West and Central Africa')
rects3
#set chart title
'Refugee in Africa region | 2018-2021', pad=70)
ax.set_title(
#set subtitle
'Number of people in millions', x=0.02, y=0.88, ha='left', va='top')
plt.suptitle(
#set x-axis tick and label
ax.set_xticks(x, labels)
#set chart legend
=(0,1.05), ncol=2)
ax.legend(loc
# set formatted data label
=[f'{x*1e-6:,.1f}' for x in rects1.datavalues], padding=3)
ax.bar_label(rects1, labels=[f'{x*1e-6:,.1f}' for x in rects2.datavalues], padding=3)
ax.bar_label(rects2, labels=[f'{x*1e-6:,.1f}' for x in rects3.datavalues], padding=3)
ax.bar_label(rects3, labels
#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()