67 of 100: Concentric circles 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: I need to add the flag to the center of the circles and automate it more.
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.
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 codes, year labels an colors 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)
df = df.sort_values(['countries' ,'sites' ], ascending=False ).reset_index(drop=True)
df['colors'] =["#5375D4","#5375D4","#2C324F","#2C324F","#CC5A43","#CC5A43"]
Define the variables
x = [-0.1,0.4,0.55]
y = [0.6,0.3, 1.2]
w = [1,0.4,0.6]
h = [1,0.4,0.6]
a =[1,2,3]
x_lbl = [-0.15,0.36,1.2]
y_lbl =[1.1,0.5,1.5]
Plot the chart
fig = plt.figure(figsize=(5, 5))
for x,y,w,h,a, country ,x_lbl, y_lbl in zip(x,y,w,h,a,df.countries.unique(),x_lbl,y_lbl):
temp_df = df[df.countries==country]
sites= temp_df.sites
year_lbl =temp_df.year_lbl
colors =temp_df.colors
ax= fig.add_axes([x, y, w, h], polar=True,)
for site, c, label in zip(sites, colors, year_lbl):
max_sites= sites.max()
ax.plot(np.linspace(0, 2*np.pi, 100), np.ones(100)*site, color=c, linestyle='-',lw=2)
ax.set_rgrids(range(0,max_sites))
ax.xaxis.grid(False)
ax.spines['polar'].set_visible(False)
ax.yaxis.grid(True,color=c,linestyle='-')
ax.patch.set_alpha(0.1) #transparent axis
ax.set_yticklabels([])
ax.set_xticklabels([])
ax.annotate(label, xy = (np.pi/2, site-0.1),color = c,ha="center", va= "center" ,
bbox=dict(facecolor='w', edgecolor='w', boxstyle='round,pad=0.2'))
ax.text( x_lbl,y_lbl, max_sites,color = c, size = 12, weight = "bold", ha="center", va= "center" ,transform=plt.gcf().transFigure)
The result:

Reader Interactions