← 返回首页
Basic Bubble Plot with matplotlib

Basic Bubble Plot with matplotlib

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 1 2 3 4
0.487913 0.640856 0.685197
0.904584 0.029056 0.289917
0.186066 0.909341 0.576339
0.560585 0.119788 0.857088
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:

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

🚨 Grab the Data To Viz poster!


Do you know all the chart types? Do you know which one you should pick? I made a decision tree that answers those questions. You can download it for free!

    Get Poster