I discovered a hidden gem in Matplotlib’s library: Packed Bubble Charts in Python | by Anna Gordun Peiro | Jul, 2024

[ad_1]

For my chart, I’m utilizing an Olympic Historic Dataset from Olympedia.org which Joseph Cheng shared in Kaggle with a public area license.

Screenshot of dataset

It comprises occasion to Athlete stage Olympic Video games Outcomes from Athens 1896 to Beijing 2022. After an EDA (Exploratory Knowledge Evaluation) I remodeled it right into a dataset that particulars the variety of feminine athletes in every sport/occasion per yr. My bubble chart concept is to indicate which sports activities have a 50/50 feminine to male ratio athletes and the way it has developed throughout time.

My plotting information consists of two totally different datasets, one for every year: 2020 and 1996. For every dataset I’ve computed the entire sum of athletes that participated to every occasion (athlete_sum) and the way a lot that sum represents in comparison with the variety of whole athletes (male + feminine) (distinction). See a screenshot of the info beneath:

Display screen shot of plotting dataset

That is my method to visualise it:

  • Measurement proportion. Utilizing radius of bubbles to check quantity athletes per sport. Larger bubbles will signify extremely aggressive occasions, similar to Athletics
  • Multi variable interpretation. Making use of colors to signify feminine illustration. Gentle inexperienced bubbles will signify occasions with a 50/50 break up, similar to Hockey.

Right here is my start line (utilizing the code and method from above):

First consequence

Some straightforward fixes: rising determine measurement and altering labels to empty if the scale isn’t over 250 to keep away from having phrases outdoors bubbles.

fig, ax = plt.subplots(figsize=(12,8),subplot_kw=dict(facet="equal"))

#Labels edited immediately in dataset

Second consequence

Properly, now at the very least it’s readable. However, why is Athletics pink and Boxing blue? Let’s add a legend for instance the connection between colors and feminine illustration.

As a result of it’s not your common barplot chart, plt.legend() doesn’t do the trick right here.

Utilizing matplotlib Annotation Bbox we will create rectangles (or circles) to indicate which means behind every color. We will additionally do the identical factor to indicate a bubble scale.

import matplotlib.pyplot as plt
from matplotlib.offsetbox import (AnnotationBbox, DrawingArea,
TextArea,HPacker)
from matplotlib.patches import Circle,Rectangle

# That is an instance for one part of the legend

# Outline the place the annotation (legend) shall be
xy = [50, 128]

# Create your coloured rectangle or circle
da = DrawingArea(20, 20, 0, 0)
p = Rectangle((10 ,10),10,10,coloration="#fc8d62ff")
da.add_artist(p)

# Add textual content

textual content = TextArea("20%", textprops=dict(coloration="#fc8d62ff", measurement=14,fontweight='daring'))

# Mix rectangle and textual content
vbox = HPacker(youngsters=[da, text], align="high", pad=0, sep=3)

# Annotate each in a field (change alpha if you wish to see the field)
ab = AnnotationBbox(vbox, xy,
xybox=(1.005, xy[1]),
xycoords='information',
boxcoords=("axes fraction", "information"),
box_alignment=(0.2, 0.5),
bboxprops=dict(alpha=0)
)
#Add to your bubble chart
ax.add_artist(ab)

I’ve additionally added a subtitle and a textual content description beneath the chart simply by utilizing plt.textual content()

Closing visualisation

Simple and consumer pleasant interpretations of the graph:

  • Majority of bubbles are gentle inexperienced → inexperienced means 50% females → majority of Olympic competitions have a fair 50/50 feminine to male break up (yay🙌)
  • Just one sport (Baseball), in darkish inexperienced color, has no feminine participation.
  • 3 sports activities have solely feminine participation however the variety of athletes is pretty low.
  • The most important sports activities by way of athlete quantity (Swimming, Athletics and Gymnastics) are very near having a 50/50 break up

[ad_2]
Anna Gordun Peiro
2024-07-28 00:02:41
Source hyperlink:https://towardsdatascience.com/i-found-a-hidden-gem-in-matplotlibs-library-packed-bubble-charts-in-python-d0f5d892beb7?source=rss—-7f60cf5620c9—4

Similar Articles

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular