Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Bubble Charts

Add a 3rd dimension using marker size to compare volume alongside x/y metrics.

What is a bubble chart?

A bubble chart is a scatter plot where marker size carries a third variable, so a single point communicates three numbers at once:

  • x axis = metric 1
  • y axis = metric 2
  • bubble size = metric 3 (volume/magnitude)

Add color and you're encoding a fourth variable too — which is exactly what px.scatter(..., size=...) gives you; a bubble chart isn't a separate chart type in Plotly, it's a scatter plot with the size argument filled in.

Example

Python
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Mumbai", "Bengaluru"],
    "sales": [120, 180, 90, 160],
    "profit": [30, 45, 10, 35],
    "orders": [200, 350, 120, 280],
})
 
fig = px.scatter(
    df,
    x="sales",
    y="profit",
    size="orders",
    color="city",
    hover_name="city",
    title="Sales vs Profit (bubble size = orders)",
)
fig.show()

Plotly scales bubble area (not raw radius) to the size column by default, which is the perceptually correct choice — doubling a value should roughly double the visual area, not the diameter.

sketch Bubble size encodes a third metric p5.js
x = sales, y = profit, bubble area = orders — bigger bubble means more orders, independent of position.

Tips

  • Keep sizes reasonable; very large bubbles hide others — cap the biggest bubble at a sensible max area.
  • Use hover tooltips (hover_name) to reduce label clutter instead of printing every city name on the chart.
  • Watch out for overlap: when bubbles are close together, consider adding opacity so overlapping regions stay readable.
diagram Bubble chart = scatter + a size channel mermaid
A bubble chart layers a third numeric variable onto marker size, and optionally a fourth onto color.

Check your understanding

quizCheck yourself
  1. What does bubble size encode in px.scatter(..., size="orders")?

    Show answer

    B — The orders column, a third variable — Marker size carries a third variable, so one point shows sales, profit and orders together.

  2. Is a bubble chart a separate chart type in Plotly Express?

    Show answer

    C — No, it is px.scatter with the size argument — A bubble chart is a scatter plot with size set, and adding color encodes a fourth variable.

  3. Plotly scales bubble area to the size column. Why is that sensible?

    Show answer

    B — Doubling a value roughly doubles the visual area — Scaling area, not raw radius, is the perceptually correct choice.

  4. Bubbles overlap heavily in a chart. Which tip from the lesson helps?

    Show answer

    A — Add opacity — Opacity keeps overlapping regions readable. Capping the largest bubble size also helps.

Next

Continue to: Sunburst Charts for visualizing hierarchical data.

🧪 Try It Yourself

Exercise 1 – Turn a Scatter into a Bubble Chart

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Reduce Label Clutter with hover_name

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Add a Fourth Variable with Color

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did