A bubble plot is a variation of a scatter chart in which bubbles represent the data points of the data, and an additional dimension of the data represents the size of the points.
Also read: Python Plotly Tutorial
Advantages and Disadvantages of Bubble Chart
Just like the scatter, a bubble chart helps to depict and show relationships between various numeric values. However, the addition of marker size (the size of the bubble ) as a dimension allows us to compare three different variables at the same time!
Along with this, it displays data in 3-D which widens the scope of the analysis for the dataset. You can also add a fourth variable to add various colors to the plot.
The only disadvantage of the chart is that at times due to bubble sizes, the chart can get difficult to read and understand. As a result, it can’t be used to display tones of data.
Code Implementation of Bubble Plot
To create a bubble chart, we need a data table containing three different columns. Where two columns will correspond with the horizontal and vertical values of the plot ( the x and y-axis ) and the third will indicate the size of the points.
Importing the Modules
We will start off by loading the Pandas
, NumPy
and Matplotlib
libraries using the code below.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Creating Dataset for Bubble Plot
We will create the x
and y
variable values. Along with this, we will also create a third
variable for the size of bubbles and a fourth
variable to add colors to the plot.
x = np.random.normal(170, 20, 100) # 100 points for Normal Distribution
y = x + np.random.normal(5, 25, 100) # Generate y values for x values generated
colors = np.random.rand(100) # Colors as the third variable
area = (25 * np.random.rand(100))**2 # Size of Bubbles as fourth variable
We will store the data of the previous step in a Pandas dataframe using the code below.
data = pd.DataFrame({
'X': x,
'Y': y,
'Colors': colors,
"bubble_size":area})
data.head()

Create a simple Scatter Plot
We will start by making a simple scatter plot with the scatter
function. We can customize the plot according to our own preferences but for now, look at the code below.
plt.scatter('X', 'Y', data=data)
plt.xlabel("X values", size=16)
plt.ylabel("Y values", size=16)
plt.title("A Simple Scatter Plot", size=18)
plt.show()

Creating Bubble Plot
We can make a bubble plot in Python using the same scatter
function where we also need to specify size argument
to define the size of the data points.
plt.scatter('X', 'Y',
s='bubble_size',
alpha=0.5,
data=data)
plt.xlabel("X values", size=16)
plt.ylabel("Y values", size=16)
plt.title("A Simple Bubble Plot", size=18)
plt.show()

Add Colors to the Plot
To make visualizations better, we will add color to the bubbles using another variable in the plot.
plt.scatter('X', 'Y',
s='bubble_size',
c='Colors',
alpha=0.5,
data=data)
plt.xlabel("X values", size=16)
plt.ylabel("Y values", size=16)
plt.title("A Simple Bubble Plot", size=18)
plt.show()

Conclusion
Bubble charts are without a doubt one of the most useful charts when you wish to display complex variables. I hope you are now clear with what Bubble Plots are and how to implement the same using the Python programming language.
Thank you for reading!