Dot plot with Python
A dot plot is a type of graph in which the dots are connected to show changes or differences. It’s an alternative to the grouped bar chart or slope chart.
More about: Dot plot
Dot plot
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle','dotplot'])
plt.style.use([
#load and reshape the data
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/dot_plot.csv')
df = df.pivot_table(index=['location','order'], columns='period', values='percent', sort=False)
df = df.reset_index()
df
#sort values in descending order
= df.sort_values(by='order', ascending=False)
ordered_df
#wrap the long labels
= ordered_df['location']
y_labels = [ '\n'.join(wrap(l, 20)) for l in y_labels ]
wrapped_label
#plot the chart
= plt.subplots()
fig, ax 'before_covid'], wrapped_label, label='before_covid')
plt.scatter(ordered_df['first_months'], wrapped_label, label='first_months')
plt.scatter(ordered_df[=wrapped_label, xmin=ordered_df['before_covid'], xmax=ordered_df['first_months'], color='#666666')
plt.hlines(y
#set chart legend
= ["Before COVID-19", "First month of the crisis"], loc=(0,1.05), ncol=2)
ax.legend(labels
#set chart title
'COVID-19 impact on poverty rates of informal workers', pad=50)
ax.set_title(
# xticks and xticklabel format
= plt.xlim(0, 1)
limit = ax.get_xticks()
vals '{:,.0%}'.format(x) for x in vals])
ax.set_xticklabels([
#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 chart
plt.show()