Lollipop chart with Python
As a variant of the bar/column chart, the lollipop chart consists of lines and dots at the end to highlight the values. Like a bar chart, a lollipop chart is used to compare categorical data.
More about: Lollipop chart
Lollipop chart
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','lollipop'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/lollipop.csv')
df
#sort value by descending order
'displaced_number',inplace=True)
df.sort_values(
#compute data array for plotting
= df.loc[:, 'country_origin'].values
x = df.loc[:, 'displaced_number'].values
y
#plot the chart
= plt.subplots()
fig, ax =x, xmin=0, xmax=y)
ax.hlines(y'o')
ax.plot(y, x,
#set x-axis limit
= plt.xlim(0,7*1e6)
xlimt
#set chart title
'People displaced across borders by country of origin | 2020')
ax.set_title(
#set x-axis label
'Displaced population (millions)')
ax.set_xlabel(
#format x-axis tick labels
def number_formatter(x, pos):
if x >= 1e6:
= '{:1.0f}M'.format(x*1e-6)
s elif x < 1e6 and x >= 1e3:
= '{:1.0f}K'.format(x*1e-3)
s else:
= '{:1.0f}'.format(x)
s return s
ax.xaxis.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()