Bubble Plot with Seaborn

logo of a chart:BubblePlot

This post explains how to produce a basic bubble plot using seaborn and make modifications such as mapping colors to variables and changing the bubble sizes by providing reproducible examples with explanations.

Libraries & Dataset

Let's start by load a few libraries and the dataset.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

gapminder = pd.read_csv(
    'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/gapminderData.csv'
)
data = gapminder.loc[gapminder.year == 2007]

Most Basic Bubble Chart

A bubble plot is basically a scatterplot with an additional dimension: size of points. Using seaborn library, a bubble plot can be constructed using the scatterplot() function. In the example, the following parameters are used to build a basic bubble plot:

  • data : Input data structure
  • x : The data position on the x axis
  • y : The data position on the y axis
  • size : Grouping variable that will produce points with different sizes
  • alpha : Transparancy ratio

Here, the gapminder data set is used and the relationship between life expectancy (y) and gdp per capita (x) of world countries is represented. The population of each country is represented through dot size.

sns.set_theme(style="darkgrid")

# use the scatterplot function to build the bubble map
sns.scatterplot(
    data=data,
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    legend=False,
    sizes=(20, 2000)
)

# show the graph
plt.show()

Control Bubble Size

You can control the size of bubbles using the sizes argument. You should pass the minimum and maximum size to use such that other values are normalized within this range.

sns.set_theme(style="darkgrid")

# use the scatterplot function
min, max = 20, 800
sns.scatterplot(
    data=data,
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    alpha=0.5,
    sizes=(min, max),
)

# show the graph
plt.show()

Bubble color

We added a third dimension to our scatterplot with the sizes of bubbles. Now, we will add a forth dimension by mapping colors to variables. Here, the continent of each country is used to control bubble color and passed to the function with the hue argument.

sns.set_theme(style="darkgrid")

# use the scatterplot function
sns.scatterplot(
    data=data,
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    hue="continent",
    alpha=0.5,
    sizes=(20, 400)
)

# show the graph
plt.show()

Going further

This post explains how to build and customize a bubble plot using the seaborn library.

You might be interested in this excellent real life use case of a bubble plot and how to use more advanced customizations of a bubble plot.

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!