Column chart with Python

Cedric Vidonne

Lei Chen

Column chart with Python

In a column chart, each category is represented by a vertical rectangle, with the height of the rectangle being proportional to the values being plotted.

More about: Column chart


Basic column chart

# import libraries
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.csv')

#compute data array for plotting
x = df['year']
y = df['displaced_number']

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

#set chart title
ax.set_title('Global IDP displacement | 2010 - 2020')

#set y-axis title
ax.set_ylabel('Displaced population (millions)')

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

#set y-axis limit
ylimit = plt.ylim(0, 100*1e6)

#set tick based on x value
ax.set_xticks(x)

#set grid
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 column chart showing the total displaced population from 2011 to 2021


Column chart with data label

# import libraries
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.csv')

#compute data array for plotting
x = df['year']
y = df['displaced_number']

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

#set chart title
ax.set_title('Global IDP displacement | 2010 - 2020')

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

#set tick based on x value 
ax.set_xticks(x)

# set formatted data label
for x,y in zip(x,y):
    label = "{:.1f}".format(y*1e-6)
    plt.annotate(label,
                 (x,y),
                 textcoords="offset points",
                 xytext=(0,6), 
                 ha='center')

#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 column chart showing the total displaced population from 2011 to 2021


Related chart with Python