Basic Bubble Plot with matplotlib

logo of a chart:BubblePlot

This post explains how to produce a basic bubble plot using matplotlib and provides a reproducible code.

In the next post, you will learn how to change colors, shape and size of the bubbles and how to map bubble colors with a 4th variable.

Libraries & Dataset

We will start by importing the necessary libraries and loading the dataset.

Since bubble plots requires numerical values, we need to have quantitative data in our dataset.

# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# create data
df = pd.DataFrame({
      'x': np.random.rand(40),
      'y': np.random.rand(40),
      'z': np.random.rand(40),
   })
df.head()
x y z
0 0.487913 0.640856 0.685197
1 0.904584 0.029056 0.289917
2 0.186066 0.909341 0.576339
3 0.560585 0.119788 0.857088
4 0.335864 0.779507 0.092400

Bubble plot

A bubble plot is very similar to a scatterplot. Using matplotlib library, a bubble plot can be constructed using the scatter() function. In the example, the following parameters are used:

  • x : The data position on the x axis
  • y : The data position on the y axis
  • s : The marker size
  • alpha : Transparancy ratio
plt.scatter(df['x'], df['y'], s=df['z']*1000, alpha=0.5)
plt.show()

Going further

You might be interested in:

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 🙏!