Grouped column chart with Python

Cedric Vidonne

Lei Chen

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
plt.style.use(['unhcrpyplotstyle','column'])

#load data set
df = pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/column_grouped.csv')

#reshape df from long to wide
df = df.pivot(index='year', columns='main_office', values='refugee_number')
df = df.reset_index()

#prepare data array for plotting
labels = df['year']
category1 = df['East and Horn of Africa and Great Lakes']
category2 = df['Southern Africa']
category3 = df['West and Central Africa']

#set x-axis ticks label location
x = np.arange(len(labels))

#set bar width
width = 0.28

#plot the chart
fig, ax = plt.subplots()
rects1 = ax.bar(x - width, category1, width, edgecolor='white', label='East and Horn of Africa and Great Lakes')
rects2 = ax.bar(x, category2, width, edgecolor='white', label='Southern Africa')
rects3 = ax.bar(x + width, category3, width, edgecolor='white', label='West and Central Africa')

#set chart title
ax.set_title('Refugee in Africa region | 2018-2021', pad=50)

#set chart legend
ax.legend(loc=(0,1.04), ncol=2)

#set y-axis title
ax.set_ylabel('Number of people (millions)')

#set y-axis label
ax.tick_params(labelleft=True)

#set x-axis tick and label
ax.set_xticks(x, labels)

#show grid below the bars
ax.grid(axis='y')

#format y-axis tick labels
def number_formatter(x, pos):
    if x >= 1e6:
        s = '{:1.0f}M'.format(x*1e-6)
    elif x < 1e6 and x >= 1e3:
        s = '{:1.0f}K'.format(x*1e-3)
    else: 
        s = '{:1.0f}'.format(x)
    return s
ax.yaxis.set_major_formatter(number_formatter)

#set chart source and copyright
plt.annotate('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)

#adjust chart margin and layout
fig.tight_layout()

#show chart
plt.show()

A grouped column chart showing refugee in Africa region | 2018-2021


Grouped column chart with data label

# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use(['unhcrpyplotstyle','column'])

#load data set
df = pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/column_grouped.csv')

#reshape df from long to wide
df = df.pivot(index='year', columns='main_office', values='refugee_number')
df = df.reset_index()

#compute data array for plotting
labels = df['year']
category1 = df['East and Horn of Africa and Great Lakes']
category2 = df['Southern Africa']
category3 = df['West and Central Africa']

#set x-axis ticks label location
x = np.arange(len(labels))

#set bar width
width = 0.28

#plot the chart
fig, ax = plt.subplots()
rects1 = ax.bar(x - width, category1, width, edgecolor='white', label='East and Horn of Africa and Great Lakes')
rects2 = ax.bar(x, category2, width, edgecolor='white', label='Southern Africa')
rects3 = ax.bar(x + width, category3, width, edgecolor='white', label='West and Central Africa')

#set chart title
ax.set_title('Refugee in Africa region | 2018-2021', pad=70)

#set subtitle
plt.suptitle('Number of people in millions', x=0.02, y=0.88, ha='left', va='top')

#set x-axis tick and label
ax.set_xticks(x, labels)

#set chart legend
ax.legend(loc=(0,1.05), ncol=2)

# set formatted data label
ax.bar_label(rects1, labels=[f'{x*1e-6:,.1f}' for x in rects1.datavalues], padding=3)
ax.bar_label(rects2, labels=[f'{x*1e-6:,.1f}' for x in rects2.datavalues], padding=3)
ax.bar_label(rects3, labels=[f'{x*1e-6:,.1f}' for x in rects3.datavalues], padding=3)

#set chart source and copyright
plt.annotate('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)

#adjust chart margin and layout
fig.tight_layout()

#show chart
plt.show()

A grouped column chart showing refugee in Africa region | 2018-2021


Related chart with Python