Ordered column chart with Python

Cedric Vidonne

Lei Chen

Ordered column chart with Python

An ordered column chart is a chart in which each category is represented by a vertical rectangle, with the height of the rectangle being ordered and proportional to the values being plotted.

More about: Ordered column chart


Basic ordered 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/ranking/column_ordered.csv')

#sort value by descending order
df.sort_values('returnee_number', ascending=False, inplace=True)

#compute data array for plotting
x = df.loc[:, 'country_origin'].values
y = df.loc[:, 'returnee_number'].values

#plot the chart
fig, ax = plt.subplots()
bar_plot = ax.bar(x, y)

#set chart title
ax.set_title('Refugee returns by country of origin (top 5) | 2020')

#set y-axis label
ax.set_ylabel('Number of people (thousands)')

#set tick parameters 
ax.tick_params(labelleft=True)

#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 > 0:
        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, -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)

#adjust chart margin and layout
fig.tight_layout()

#show chart
plt.show()

An ordered column showing refugee returns by country of origin (top 5) | 2020


Ordered 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/ranking/column_ordered.csv')

#sort value in descending order
df.sort_values('returnee_number', ascending=False, inplace=True)

#prepare data array for plotting
x = df.loc[:, 'country_origin'].values
y = df.loc[:, 'returnee_number'].values

#plot the chart
fig, ax = plt.subplots()
bar_plot = ax.bar(x, y)

#set chart title
ax.set_title('Refugee returns by country of origin (top 5) | 2020', pad=40)

#set subtitle
plt.suptitle('Number of people (thousands)', x=0.022, y=0.88, ha='left')

# set data label
ax.bar_label(bar_plot, labels=[f'{x:,.0f}' for x in bar_plot.datavalues])

#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 charts
plt.show()

An ordered column showing refugee returns by country of origin (top 5) | 2020


Related chart with Python