87 of 100: Proportional area charts 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 matplotlib as mpl
from svgpathtools import svg2paths
from svgpath2mpl import parse_path
import numpy as np
import pandas as pdGenerate 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 rank by year and sites and then sort the data.
df = df.sort_values(['year' ,'countries', 'sites' ], ascending = False ).reset_index(drop=True)
df["rank"] = df.groupby("year")["sites"].rank().astype(int)Create the heritage symbol
icon_path, attributes = svg2paths('flags/Unesco_World_Heritage_logo_notext_transparent.svg')
#matplotlib path object of the icon
icon_marker = parse_path(attributes[0]['d'])
icon_marker.vertices -= icon_marker.vertices.mean(axis=0)
icon_marker = icon_marker.transformed(mpl.transforms.Affine2D().rotate_deg(180))
icon_marker = icon_marker.transformed(mpl.transforms.Affine2D().scale(-1,1))Define the variables
countries = df.countries.unique()
sites = df.sites
years = df.year.unique()
X, Y = np.meshgrid(np.arange(3), np.arange(2))
colors = ["#5375D4","#2B314D","#A54836",]*2Plot the chart
fig, ax = plt.subplots(figsize=(10,6), facecolor = "#FFFFFF")
ax.scatter(X,Y, s=sites*800, alpha=0.9, color = colors, zorder = 0)
ax.scatter(X,Y,s=1000,marker=icon_marker, color = colors, zorder=1)
for i, site in enumerate(sites):
ax.annotate(site, (X.flatten()[i], Y.flatten()[i]-0.4), ha= "center", va="center", size= 14, color= "k", weight = "bold")
ax.set_xlim(-0.5,2.5)
ax.set_ylim(-0.5,1.5)
ax.tick_params(axis='both', which='major',labeltop=True,labelbottom=False,length=0,labelsize=12,colors= '#757C85')
plt.box(False)
ax.yaxis.set_ticks(range(len(years)), labels =years)
ax.xaxis.set_ticks(range(len(countries)), labels = countries) The result:

Reader Interactions