Print

98 of 100: Index 100 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 numpy as np 
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

Generate the data

The data is generated in lists to control how the curves look like:

x = [[0, 10, 125, 150],[0, 15, 125, 150],[0, 15, 130, 150]]
y = [[0, 0, 140, 150],[0,0,55,60],[0,0,14,15]]

Define the variables

img = [plt.imread("flags/de-rd.png"), plt.imread("flags/no-rd.png"), plt.imread("flags/sw-rd.png"), plt.imread("flags/de-rd.png"),  plt.imread("flags/sw-rd.png"), plt.imread("flags/no-rd.png"),]

Plot the chart

fig, ax = plt.subplots(figsize=(8,8), facecolor = "#FFFFFF" )
#to control which colors matplotlib cycles through
ax.set_prop_cycle(color =["#A54836", "#2B314D", "#5375D4"])

for x,y in zip( x,y):
    x=np.array(x)
    y=np.array(y)
    
    x_new = np.linspace(x.min(), x.max(),500)
    
    f = interp1d(x, y, kind='quadratic')
    y_smooth=f(x_new)
    
    ax.plot (x_new,y_smooth, lw=4)
    #ax.scatter (x, y) to ssee where to smooth the lines

#add the year labels lines
ax.axvline(x=0, ymin=0, ymax=0.05,lw=1,zorder=0, color= "#DFE3E5")
ax.axvline(x=150, ymin=0, ymax=0.95,lw=1,zorder=0, color= "#DFE3E5")

#add the year labels
ax.annotate( "'04", xy=(-3, -15),size = 16,color = "#9EA6AC", annotation_clip=False)
ax.annotate( "'22", xy=(147, -15),size = 16,color = "#9EA6AC", annotation_clip=False)

#add data labels
ax.annotate( "+150%", xy=(165, 150-2),size = 16,color = "#444E56", weight= "bold", annotation_clip=False)
ax.annotate( "+60%", xy=(165, 60-2),size = 16,color = "#444E56", weight= "bold", annotation_clip=False)
ax.annotate( "+15.4%", xy=(165, 15-2),size = 16,color = "#444E56", weight= "bold",annotation_clip=False)

ax.set_axis_off()

def flags(img, x,y):
    
    image_box =  OffsetImage(img, zoom = 0.05) #container for the image
    ab = AnnotationBbox(image_box, (1,1), xybox= (x,y),  frameon = False)
    ax.add_artist(ab)

positionx = [158]*3 + [-8,-14,-22]
positiony = [150,60,15] + [0]*3

for im, posx, posy in zip(img, positionx,positiony):
    flags(im, posx, posy)

The result:

98 of 100: Index 100 chart in matplotlib
Was this helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents