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
'unhcrpyplotstyle','column'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/column_ordered.csv')
df
#sort value by descending order
'returnee_number', ascending=False, inplace=True)
df.sort_values(
#compute data array for plotting
= df.loc[:, 'country_origin'].values
x = df.loc[:, 'returnee_number'].values
y
#plot the chart
= plt.subplots()
fig, ax = ax.bar(x, y)
bar_plot
#set chart title
'Refugee returns by country of origin (top 5) | 2020')
ax.set_title(
#set y-axis label
'Number of people (thousands)')
ax.set_ylabel(
#set tick parameters
=True)
ax.tick_params(labelleft
#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 > 0:
= '{: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, -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()
Ordered 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/ranking/column_ordered.csv')
df
#sort value in descending order
'returnee_number', ascending=False, inplace=True)
df.sort_values(
#prepare data array for plotting
= df.loc[:, 'country_origin'].values
x = df.loc[:, 'returnee_number'].values
y
#plot the chart
= plt.subplots()
fig, ax = ax.bar(x, y)
bar_plot
#set chart title
'Refugee returns by country of origin (top 5) | 2020', pad=40)
ax.set_title(
#set subtitle
'Number of people (thousands)', x=0.022, y=0.88, ha='left')
plt.suptitle(
# set data label
=[f'{x:,.0f}' for x in bar_plot.datavalues])
ax.bar_label(bar_plot, 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 charts
plt.show()