Grouped bar chart with Python
Grouped bar charts are a type of colour-coded bar chart that is used to represent and compare different categories of two or more groups.
More about: Grouped bar chart
Basic grouped bar chart
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle', 'bar'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/bar_grouped.csv')
df
#reshape df from long to wide
= df.pivot(index='region', columns='year', values='asylum_application')
df = df.reset_index()
df
#sort value by descending order
= df.sort_values(by=['region'], ascending=False)
df
#compute data array for plotting
= df['region']
labels = df[2019]
category1 = df[2020]
category2
#wrap long labels
= [ '\n'.join(wrap(l, 20)) for l in labels ]
labels
#set x-axis ticks label location
= np.arange(len(labels))
x
#set bar width
= 0.4
width
#plot the chart
= plt.subplots()
fig, ax = ax.barh(x + width/2, category2, width, edgecolor='white', label=2020)
rects1 = ax.barh(x - width/2, category1, width, edgecolor='white', label=2019)
rects2
#set chart title
'Individual asylum applications registered by region | 2019-2020')
ax.set_title(
#set chart legend
=(0,1), ncol=2)
ax.legend(loc
#set y-axis title
'Number of people')
ax.set_xlabel(
#set y-axis label
=True)
ax.tick_params(labelbottom
#set x-axis label
ax.set_yticks(x, labels)
#set x-axis limit
= plt.xlim(0, 1000000)
xlimit
#show grid below the bars
='x')
ax.grid(axis
#format x-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.xaxis.set_major_formatter(number_formatter)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -40), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -50), 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 bar chart with data label
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle', 'bar'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/bar_grouped.csv')
df
#reshape df from long to wide
= df.pivot(index='region', columns='year', values='asylum_application')
df = df.reset_index()
df
#sort value in descending order
= df.sort_values(by=['region'], ascending=False)
df
#prepare data array for plotting
= df['region']
labels = df[2019]
category1 = df[2020]
category2
#wrap long labels
= [ '\n'.join(wrap(l, 20)) for l in labels ]
labels
#set x-axis ticks label location
= np.arange(len(labels))
x
#set bar width
= 0.4
width
#plot the chart
= plt.subplots()
fig, ax
= ax.barh(x + width/2, category2, width, edgecolor='white', label=2020)
rects1 = ax.barh(x - width/2, category1, width, edgecolor='white', label=2019)
rects2
#set chart title
'Individual asylum applications registered by region | 2019-2020', pad=50)
ax.set_title(
#set subtitle
'Number of people in thousands', x=0.36, y=0.885)
plt.suptitle(
#set chart legend
=(0,1), ncol=2)
ax.legend(loc
#set formatted data label
=[f'{x*1e-3:,.0f}' for x in rects1.datavalues], padding=3)
ax.bar_label(rects1, labels=[f'{x*1e-3:,.0f}' for x in rects2.datavalues], padding=3)
ax.bar_label(rects2, labels
#set x-axis tick and label
ax.set_yticks(x, labels)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -10), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -20), 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()