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()
xyz
00.4879130.6408560.685197
10.9045840.0290560.289917
20.1860660.9093410.576339
30.5605850.1197880.857088
40.3358640.7795070.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

Basic Bubble Plot with matplotlib: Bubble plot

plt.scatter(df['x'], df['y'], s=df['z']*1000, alpha=0.5)
plt.show()

Going further

You might be interested in: