10 of 100: Scatter chart in a diagonal 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!
This is the original viz that we are trying to recreate in matplotlib:

Import the packages
We will need matplotlib, and pandas.
We will use matplotlib.patches to create the arcs and math to do some calculations.
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import math
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", }
code_dict = {"Norway": "NO", "Denmark": "DK", "Sweden": "SE", }
xy_ticklabel_color, grid_color, datalabels_color ='#757C85', "#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 |
I need to add two columns to the dataframe: country code and year labels.
df = df.sort_values([ 'year'], ascending=True ).reset_index(drop=True)
df['year_lbl'] ="'"+df['year'].astype(str).str[-2:].astype(str)
#map the colors of a dict to a dataframe
df['colors']= df.countries.map(color_dict)
df['ctry_codes']= df.countries.map(code_dict)
index | year | countries | sites | ctry_code | year_lbl | color |
---|---|---|---|---|---|---|
0 | 2004 | Denmark | 4 | DK | ’04 | #A54836 |
1 | 2004 | Norway | 5 | NO | ’04 | #2B314D |
2 | 2004 | Sweden | 13 | SE | ’04 | #5375D4 |
3 | 2022 | Denmark | 10 | DK | ’22 | #A54836 |
4 | 2022 | Norway | 8 | NO | ’22 | #2B314D |
5 | 2022 | Sweden | 15 | SE | ’22 | #5375D4 |
Define the variables:
sites = df.sites
lbl1 = df.year_lbl
countries =df.ctry_codes
colors =df.colors
Plot the chart:
fig, ax = plt.subplots( figsize=(6,6),sharex=True, sharey=True, facecolor = "#FFFFFF", zorder= 1)
ax.scatter(sites, sites, s= 340, c= colors , zorder = 1)
ax.set_xlim(0, sites.max()+3)
ax.set_ylim(0, sites.max()+3)
ax.axline([ax.get_xlim()[0], ax.get_ylim()[0]], [ax.get_xlim()[1], ax.get_ylim()[1]], zorder = 0, lw=1, color =xy_ticklabel_color )
for i, l1, site in zip(range(0,6), lbl1,sites) :
ax.annotate(l1, (sites[i], sites[i]), color = datalabels_color, va= "center", ha = "center")
ax.annotate(site, (sites[i]+1, sites[i]), color = xy_ticklabel_color,va= "center", ha = "center")
pos_labels =[(1.3,-1.4),(0.5,-0.8),(0.5,-0.6)]
#add the arcs
for x1, x2, pos_lbl, color, country in zip(sites[:len(sites) // 2], sites[len(sites) // 2:], pos_labels, colors, countries):
ax.annotate(country, (x1, x2),xytext=(x1+pos_lbl[0], x2+pos_lbl[1]), size = 10, weight = "bold", color=color, va="baseline", ha="center",
bbox=dict(boxstyle="round,pad=0.2", ec= "w", facecolor="w", lw=2))
ax.annotate("", xy=(x1,x1), xytext=(x2,x2), zorder = 1,
arrowprops=dict(arrowstyle='-', connectionstyle='arc3,rad=0.6',
color = color, linewidth=2, linestyle='-', antialiased=True))
ax.set_axis_off()
The result:

Reader Interactions