Print

3 of 100: Lollipoll 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: In the future, I will try to add the colors to a dictionary and feed them to matplotlib, rather than having them in the dataframe.

This is the original viz that we are trying to recreate in matplotlib:

Import the packages

We will need matplotlib, and pandas.

import matplotlib.pyplot as plt
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 = {(2004,"Norway"): "#9194A3", (2022,"Norway"): "#2B314D",
              (2004,"Denmark"): "#E2AFA5", (2022,"Denmark"): "#A54836",
              (2004,"Sweden"): "#C4D6F8", (2022,"Sweden"): "#5375D4",
              }

xy_ticklabel_color, xlabel_color, grand_totals_color, grid_color, datalabels_color ='#C8C9C9',"#101628","#101628", "#C8C9C9", "#2B314D"

data = {
    "year": [2004, 2022, 2004, 2022, 2004, 2022],
    "countries" : ["Sweden", "Sweden", "Denmark", "Denmark", "Norway", "Norway"],
    "sites": [13,15,4,10,5,8]
}
df= pd.DataFrame(data)
Indexyearcountriessites
02004Sweden13
12022Sweden15
22004Denmark4
32022Denmark10
42004Norway5
52022Norway8

Then we need to add: the x labels (year_lbl), the x axis title (Pct_change) and the color for each bar or lolipoll.

#custom sort
sort_order_dict = {"Denmark":2, "Sweden":3, "Norway":1, 2004:4, 2022:5}
df = df.sort_values(by=['year','countries',], key=lambda x: x.map(sort_order_dict))

# Add the x-axis labels
df['year_lbl'] ="'"+df['year'].astype(str).str[-2:].astype(str)
df['pct_change'] = df.groupby('countries', sort=False)['sites'].apply(
     lambda x: x.pct_change()).to_numpy()

#Add the color based on the color dictionary
df['color'] = df.set_index(['year', 'countries']).index.map(color_dict.get)
yearcountriessitesyear_lblpct_changecolor
42004Norway5’04NaN#9194A3
22004Denmark4’04NaN#E2AFA5
02004Sweden13’04NaN#C4D6F8
52022Norway8’220.600000#2B314D
32022Denmark10’221.500000#A54836
12022Sweden15’220.153846#5375D4

Define the variables:

#number of countries to loop over
countries = df.countries.unique()

Plot the lolipoll chart

Loop over countries to get one plot per country:


fig, axes = plt.subplots(ncols=len(countries), nrows=1, figsize=(8,6), sharex=True, sharey=True, facecolor= "white")
fig.tight_layout(pad=3.0)

#loop over the countries
for ctry , ax in zip(countries, axes.ravel()):
    temp_df = df[df.countries==ctry]
    pct = temp_df['pct_change'].max()
   
   #format the plots
    ax.set_ylim(0,df.sites.max()+5)
    ax.set_xlim(-0.5,len(countries)-1.5)
    ax.set_yticks([])
    ax.tick_params(axis='both', which='both',length=0, labelsize=12,colors =xy_ticklabel_color)
    ax.spines[['top', 'left', 'right']].set_visible(False)
    ax.spines['bottom'].set_color(grid_color)

    #add the circles at the end of the lolipoll bars
    ax.scatter(temp_df.year_lbl, temp_df.sites, s=150, c= temp_df.color , edgecolors="w", zorder=2)

    #add the vertical lines of the lolipoll
    ax.vlines(list(range(len(countries)-1)), 0,temp_df.sites, color = temp_df.color, lw=4,zorder=1)
    
    #add the data labels
    for i,lb in enumerate(temp_df.sites):
        ax.annotate(lb, xy=(i,lb+1), size=13,color =datalabels_color, weight= "bold", ha="center", va="center")

    #add the x-axis titles
    ax.set_xlabel(f'\u25B2\n{pct:.0%}\n\n{ctry}',  color = xlabel_color, size = 12, weight= "bold")
    ax.xaxis.set_label_coords(0.5, -0.1)

The result:

3 of 100: Lolipoll 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