37 of 100: Tabular circle 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!
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
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 = {2022: "#CC5A43", 2004: "#5375D4", }
xy_ticklabel_color, datalabels_color ='#757C85',"#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 create the country code and year labels and then sort the data.
df['ctry_code'] = df.countries.astype(str).str[:2].astype(str).str.upper()
df['year_lbl'] ="'"+df['year'].astype(str).str[-2:].astype(str)
sort_order_dict = {"Denmark":2, "Sweden":3, "Norway":1, 2004:4, 2022:5}
df = df.sort_values(by=['countries','year',], key=lambda x: x.map(sort_order_dict))
df['color']= df.year.map(color_dict)
year | countries | sites | ctry_code | year_lbl | color | |
---|---|---|---|---|---|---|
2 | 2004 | Norway | 5 | NO | ’04 | #5375D4 |
3 | 2022 | Norway | 8 | NO | ’22 | #CC5A43 |
0 | 2004 | Denmark | 4 | DE | ’04 | #5375D4 |
1 | 2022 | Denmark | 10 | DE | ’22 | #CC5A43 |
4 | 2004 | Sweden | 13 | SW | ’04 | #5375D4 |
5 | 2022 | Sweden | 15 | SW | ’22 | #CC5A43 |
Define the variables
years = df.year.unique()
countries = df.countries.unique()
sites = df.sites
colors = df.color
Plot the chart
fig, axes = plt.subplots(ncols=2,nrows=3,figsize=(5,5),sharex=True, sharey=True, facecolor = "#FFFFFF")
for ax, color, site in zip(axes.ravel(), colors*3, sites):
x=list(np.arange(0,site,1))
y=list(reversed(np.arange(16- site,16,1)))
ax.plot(x, y, '-', lw = 30, color = color, solid_capstyle="round",zorder=0 )
ax.scatter(0,15,s = 860,color= '#2C324F', zorder=2)
ax.annotate(site, (0,15),size= 12, color = datalabels_color, ha ="center", va ="center")
ax.set_yticklabels([])
ax.set_xticklabels([])
ax.spines[['left','right','bottom','top']].set_visible(False)
ax.tick_params(length=0)
ax.set_xlim(-4,20)
ax.set_ylim(-4,22)
for ax, year in zip(axes[:,0], countries):
ax.set_ylabel(year, rotation=0,size= 12, color = xy_ticklabel_color)
ax.yaxis.set_label_coords(-0.5, 0.5)
for ax, col in zip(axes[0], years):
ax.set_title(col, x=0.5, y=1.2, color = xy_ticklabel_color)
The result:

Reader Interactions