GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All
  • Best
  • Libs
  • Learn

This view

  • Original page
  • New address

GeezerVison is showing python-graph-gallery.com — Python Bubble Chart Examples with Code

Bubble plot

logo of a chart:BubblePlot

A bubble plot is a scatterplot where the circle size is mapped to the value of a third numeric variable. This section shows many bubble plots made with Python, using both the Matplotlib and Seaborn libraries.

Bubble sectionAbout this chart

⏱ Quick start

The scatterplot() function of seaborn also allows to build bubble charts. Indeed, it has a size parameter that controls circle size according to a numeric variable of the dataset.🔥

Basic bubble chart with Python and Seaborn.

Basic bubble chart with Python and Seaborn.

# libraries
import matplotlib.pyplot as plt
import seaborn as sns
from gapminder import gapminder # data set

# data
data = gapminder.loc[gapminder.year == 2007]

# use the scatterplot function to build the bubble map
sns.scatterplot(data=data, x="gdpPercap", y="lifeExp", size="pop", legend=False, sizes=(20, 2000))

# show the graph
plt.show()

Seaborn logoBubble plot with Seaborn

Seaborn is the best tool to quickly build a quality bubble chart. The example below are based on the famous gapminder dataset that shows the relationship between gdp per capita, life expectancy and population of world countries.

The examples below start simple by calling the scatterplot() function with the minimum set of parameters. It then show how to change bubble colors to represent a fourth variable, improve general styling, tweak the legend and more.

Basic bubble chart with Python and Seaborn.

Basic bubble chart with Python and Seaborn.

⚠️ Avoid the 61 most common dataviz mistakes

Boxplots hiding information, pie charts, cut axes, spaghetti charts... I gathered the 61 dataviz caveats I see the most as a deck of cards. Each card shows a chart that is not ideal, and a better alternative.

Get the full deck for free:

I respect your privacy. Unsubscribe anytime.

Dataviz caveat card: Z-index matters
61
cards

🔎 scatterplot() function parameters → see full doc

→ Description

The scatterplot() function of seaborn creates a scatter plot to visualize the relationship between two continuous variables. It displays each observation as a point on a two-dimensional plane.

→ Arguments

Description

Dataframe-like (pandas, numpy, polars...) with the columns we want to plot.

Possible values → dataframe

It just has to be a pandas.DataFrame (columns are variables), numpy.ndarray (rows/columns are variables), or any mapping/sequence (dictionaries/lists)

Supports both long-form (each variable in its own column) and wide-form (variables in separate columns; reshaped internally).

Code Example

# Library & Dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Plot
sns.scatterplot(
  data=df,
  x='sepal_length',
  y='sepal_width'
)
plt.show()
More about the scatterplot() function.

More about the scatterplot() function.

Matplotlib logoBubble plot with Matplotlib

As for scatterplots, Matplotlib will help us build a bubble plot thanks to the the plt.scatter() function. This function provides a s parameter allowing to pass a third variable that will be mapped to the markers size.

Most basic bubble plot with Python and Matplotlib

Most basic bubble plot with Python and Matplotlib

Bubble plot customization: color

Bubble plot customization: color

Bubble plot customization: shape

Bubble plot customization: shape

Bubble plot customization: size

Bubble plot customization: size

Bubble plot customization: stroke

Bubble plot customization: stroke

Note that it is a common practice to map a fourth variable to the markers colors thanks to the c parameter. This way, you're now looking a 4 variables in the same time, on the same chart 🎉.

A bubble chart width 4 dimensions: x, y, size & color

A bubble chart width 4 dimensions: x, y, size & color

Bubble plot of the gapminder dataset

Bubble plot of the gapminder dataset

A very common task when it comes to bubble chart is to add a proper legend to explain what colors and sizes mean. The blogpost below is a deep-dive into matplotlib legend and should be of great help for this

Legend customization: how to control location, add a background, customize labels and markers and more

Legend customization: how to control location, add a background, customize labels and markers and more

Matplotlib logoBest python bubble chart examples

The web is full of astonishing charts made by awesome bloggers, (often using R). The Python graph gallery tries to display (or translate from R) some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!

Learn how to use the adjustText library to add many labels with no overlaps.

Learn how to use the adjustText library to add many labels with no overlaps.

Scatterplot with regression fit, group colors and non-overlapping labels.

Scatterplot with regression fit, group colors and non-overlapping labels.

Dark mode scatterplot with custom annotations, title and explanations. All of that with a very good appearance!

Dark mode scatterplot with custom annotations, title and explanations. All of that with a very good appearance!

A scatter plot with images inside each marker to provide additional context

A scatter plot with images inside each marker to provide additional context

A scatter plot with custom annotations and colors, with some markers being circled

A scatter plot with custom annotations and colors, with some markers being circled

Related chart types

logo of a chart:ScatterPlot

Scatterplot

logo of a chart:Heatmap

Heatmap

logo of a chart:Correlogram

Correlogram

logo of a chart:BubblePlot

Bubble

logo of a chart:ScatterConnected

Connected Scatter

logo of a chart:2dDensity

2D Density

300 projects

💡 Out of dataviz inspiration?

I have spent years collecting the most beautiful data visualizations out there. Get the list of my top 300 dataviz projects, hand-picked, for free.

I respect your privacy. Unsubscribe anytime.

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.