18 of 100: Arc chart in matplotlib
At the beginning of the year I challenged myself to create all 100 visualizations using python and matplotlib from the 1 dataset,100 visualizations project and I am sharing with you the code for all the visualizations.
Note: Data Viz Project is copyright Ferdio and available under a Creative Commons Attribution – Non Commercial – No Derivatives 4.0 International license. I asked Ferdio and they told me they used a Design tool to create all the plots.
Collaborate
There are a ton of improvements that can be made on the code, so let me know in the comments any improvements you make and I will update the post accordingly!
To be Improved: The curves are hardcoded. It can be automated. Labels and colors can be improved too. Will revise later.
This is the original viz that we are trying to recreate in matplotlib:

Import the packages
We will need the following packages:
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import numpy as np
import pandas as pd
Generate the data
We could actually go from numpy to matplotlib, but most data projects use pandas to transform the data, so I am using a pandas dataframe as the starting point.
color_dict = {"Norway": "#2B314D", "Denmark": "#A54836", "Sweden": "#5375D4", }
xy_ticklabel_color, xy_lablel_color, grid_color, datalabels_color ='#C8C9C9',"#9BA0A6", "#C8C9C9", "#FFFFFF"
data = {
"year": [2004, 2022, 2004, 2022, 2004, 2022],
"countries" : [ "Denmark", "Denmark", "Norway", "Norway","Sweden", "Sweden",],
"sites": [4,10,5,8,13,15]
}
df= pd.DataFrame(data)
index | year | countries | sites |
---|---|---|---|
0 | 2004 | Sweden | 13 |
1 | 2022 | Sweden | 15 |
2 | 2004 | Denmark | 4 |
3 | 2022 | Denmark | 10 |
4 | 2004 | Norway | 5 |
5 | 2022 | Norway | 8 |
We need to sort and add the colors:
df = df.sort_values(['year' ], ascending=True ).reset_index(drop=True)
#map the colors of a dict to a dataframe
df['color']= df.countries.map(color_dict)
index | year | countries | sites | color |
---|---|---|---|---|
0 | 2004 | Denmark | 4 | #A54836 |
1 | 2004 | Norway | 5 | #2B314D |
2 | 2004 | Sweden | 13 | #5375D4 |
3 | 2022 | Denmark | 10 | #A54836 |
4 | 2022 | Norway | 8 | #2B314D |
5 | 2022 | Sweden | 15 | #5375D4 |
Define the variables
Not many variables yet, it will increase when I automate the plot more.
colors = df.color.unique()
countries = df.countries.unique()
Plot the chart
fig, ax = plt.subplots()
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
ax.invert_yaxis()
ax.set(xlim=[0, 16], ylim=[15, 0])
ax.set_xlabel(df.year.max(), size = 14, color = xy_lablel_color, weight = "bold")
ax.set_ylabel(df.year.min(), size=12, color = xy_lablel_color, weight= "bold")
ax.spines[['left', 'top']].set_color(grid_color)
ax.spines[['bottom', 'right']].set_color('w')
ax.tick_params(axis='both', which='major',length=0, labelsize=12,colors= xy_ticklabel_color)
ax.xaxis.set_ticks(np.arange(0, 20, 5), )
ax.yaxis.set_ticks(np.arange(0, 20, 5), )
for color, country in zip(colors, countries):
sites = df[df.countries == country]['sites'].to_numpy()
verts = [(sites[1],0), (sites[1],sites[0]), (0,sites[0])]
codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3]#, Path.CLOSEPOLY]
p = Path(verts,codes)
ax.add_patch(PathPatch(p, fc= "none", color= color, lw=4))
#add labels
ax.text(12, 10 , "SE", size = 14, weight ="bold", color = colors[2])
ax.text( 3, 3.5 ,"DK", size = 14, weight ="bold", color = colors[0])
ax.text( 5, 5 , "NO", size = 14, weight ="bold", color = colors[1])
The result:

Reader Interactions