Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 42 ยท Basic Charts

Scatter and Bubble Charts

Make scatter plots and bubble charts in Plotly: map columns to color, size and symbol, add hover data, text labels, marginal plots, color scales, log axes and WebGL for big data.

A scatter plot places one marker per row of data, using two numeric columns as the position. It answers the question "are these two variables related?" With color, size and symbol you can add three or four more variables, and with hover text you can show the rest. When marker size carries a variable, the chart is called a bubble chart.

In Plotly both are the same trace type, scatter, drawn with mode="markers".

A scatter plot with groups

Python Scatter plot with color and hover

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

hover_data adds extra columns to the tooltip, which Express stores in customdata and shows through a hovertemplate. hover_name sets the bold header line of the tooltip, which is handy for identifying a point (a country, a customer, a product).

Bubble charts: size as a variable

Add size="column" to turn the dots into bubbles whose area is proportional to the column. Because big bubbles can hide each other, tune size_max (the largest bubble diameter in pixels) and opacity:

A bubble chart of countries

With graph objects, bubble size is set by marker.size (an array), with sizemode="area" and sizeref to scale it. Express does this arithmetic for you through size and size_max. The marker color here is a number, so it is mapped through a colorscale and a color bar is shown.

Python Express bubble chart of the Gapminder data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

log_x=True switches the x axis to logarithmic scale, which is the right choice for quantities such as income that span several orders of magnitude. hover_name="country" tells you what each bubble is.

Marginal plots and trendlines

Two small extras make scatter plots far more informative:

  • Marginal plots: marginal_x="histogram" (or box, violin, rug) draws the distribution of each variable above and beside the main plot.
  • Trendlines: trendline="ols" adds a least-squares fit line; "lowess" adds a smooth local fit; "expanding" and "rolling" also exist. OLS and LOWESS need the statsmodels package. They are covered in depth in Trendlines and Error Bars.
Python
px.scatter(tips, x="total_bill", y="tip", color="smoker",
           marginal_x="box", marginal_y="violin", trendline="ols")

Text labels and symbols

Use text="column" to print a label next to every marker, and set textposition to place it. Labels get crowded fast, so use them for a handful of points. Marker symbol helps people who cannot distinguish colors, or when the chart is printed in black and white:

Python Labels, symbols and a color scale

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because color="rain" is numeric, Express does not split the data into traces. It creates a single shared color axis (layout.coloraxis) with one color bar. Only symbol splits the data here, since it is categorical.

Many points: WebGL

Normal scatter traces draw each point as an SVG element, and browsers struggle beyond roughly 10,000 points. Switch to WebGL, which draws on the GPU:

Python
px.scatter(big_df, x="a", y="b", render_mode="webgl")   # Express
go.Scattergl(x=..., y=..., mode="markers")               # graph objects

WebGL has a few feature differences (some marker symbols and text options are limited), but it handles hundreds of thousands of points smoothly. For millions of points, aggregate first with px.density_heatmap (see Heatmaps and Contour Plots).

Reading a scatter plot

  • Look for the shape: a rising cloud suggests positive association, a flat cloud suggests none, a curve suggests a non-linear relationship.
  • Overplotting hides density: lower opacity or use marginal plots.
  • Correlation is not causation: a trendline describes, it does not explain.
  • Use a log axis when values span orders of magnitude, and say so in the axis title.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which argument turns a scatter plot into a bubble chart?

    Mapping a numeric column to size makes marker area proportional to the values.
  2. 2. What does px.scatter(..., color="rain") do when rain is a numeric column?

    Numeric colors use a continuous scale; categorical (text) columns split into discrete traces.
  3. 3. Which option adds a distribution plot above and beside a scatter plot?

    Marginal plots show how each variable is distributed along its own axis.
  4. 4. Your scatter plot is slow with 300,000 points. What helps most?

    WebGL rendering uses the graphics card and scales to very large point counts.
  5. 5. Why use log_x=True for GDP per capita?

    A logarithmic axis spreads out data that ranges from hundreds to tens of thousands.

Summary

  • Scatter plots use mode="markers"; px.scatter maps columns to position, color, size and symbol.
  • Bubble charts add size; tune with size_max or sizeref.
  • Use hover_name, hover_data and text to identify points, and marginal_x/y and trendline to add context.
  • Numeric color uses a color axis with a color bar; categorical color splits traces.
  • Switch to WebGL (render_mode="webgl" or go.Scattergl) for large data.

Next: Bar Charts