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:
- how to change colors, shape and size of the bubbles
- how to map bubble colors with a 4th variable






