How to Create a Bubble Plot with Matplotlib

12.18.2020

Intro

A bubble plot is similar to a scatter plot, but each point is a bubble that can change size or color based on other properties. This helps you convey extra information than just a normal scatter plot. In this article we will learn how to create a bubble plot in MatPlotLib.

Creating a Bubble Plot

In a previous example, we plotted house size vs house price. Let's add a few more features to see how we can use a bubble plot. We can use house locations (0 for ocean front, 1 for rural) and HOA costs for area. This will color our bubbles based on the two location types we use and change the size of them based on the HOA costs.

import matplotlib.pyplot as plt

houseSqFt = [1000, 1500, 2000, 25000]
price = [100000, 150000, 200000, 250000]
colors = [0, 0, 1, 1]
area = [100, 110, 250, 350]

plt.scatter(houseSqFt, price,
   s = area,
   c = colors,
   alpha = 0.5)
plt.show()