Ordered bar chart with Python

Cedric Vidonne

Lei Chen

Ordered bar chart with Python

An ordered bar chart is a chart in which each category is represented by a horizontal rectangle, with the length of the rectangle being ordered and proportional to the values being plotted.

More about: Ordered bar chart


Basic ordered bar chart

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

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

#sort value by descending order
df.sort_values('returnee_number',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.barh(x, y)

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

#set x-axis label
ax.set_xlabel('Number of people (thousands)')

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

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

#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.xaxis.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 bar showing refugee returns by country of origin (top 5) | 2020


Ordered bar chart with data label

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

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

#sort value in descending order
df.sort_values('returnee_number', 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.barh(x, y)

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

#set subtitle
plt.suptitle('Number of people (thousands)', x=0.145, 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, -15), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('© UNHCR, The UN Refugee Agency', (0,0), (0, -25), 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 bar showing refugee returns by country of origin (top 5) | 2020


Related chart with Python