Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 31 ยท Production and Performance

Large Datasets and Performance

Keep interactive charts fast with millions of rows: WebGL, canvas versus SVG, LTTB downsampling, server-side binning and Datashader-style aggregation.

A chart with a few hundred points is effortless. A chart with a few million points can freeze the browser tab, produce a 200 MB HTML file, or fail with an error before it draws anything. Interactive libraries send your data to the browser and draw one mark per row, and browsers are not built to draw millions of separate shapes.

The fix is almost never "a faster library". It is to send less, draw cheaper, or summarize first. This lesson teaches the four techniques that cover nearly every case: cheaper drawing (canvas and WebGL), downsampling (keep a few points that preserve the shape), aggregation (bin points into cells), and server-side work (do both in Python before the browser sees anything). Plotly, Bokeh and Altair calls are read-only here because they need Python and a display. The runnable examples are stand-ins written in JavaScript: they implement the same algorithms, so you can measure what they do. First read How a Chart Works Inside if marks and scales are new.

Where the time goes

Three costs add up, and each has a different cure:

flowchart LR
    A["Python<br/>DataFrame"] --> B["Serialize<br/>to JSON"]
    B --> C["Download<br/>into browser"]
    C --> D["Create one<br/>mark per row"]
    D --> E["Draw and<br/>redraw on zoom"]

Every stage grows with the number of rows. Reducing rows early helps all of them; faster drawing helps only the last.

  • Size: a million rows of two floats is tens of megabytes of JSON. This slows the download and bloats saved HTML files.
  • Marks: SVG creates a DOM element per mark. Thousands are fine; hundreds of thousands make the page sluggish even before you interact.
  • Redraw: zooming or hovering redraws the whole chart, so the per-frame cost matters more than the one-time cost.

Canvas, SVG and WebGL

Browsers offer three ways to draw, and the libraries expose them as options.

Technique How it draws Good up to roughly Trade-off
SVG One DOM element per mark Thousands of marks Crisp, styleable, accessible; slow with many marks
Canvas Pixels on one bitmap, drawn by the CPU Tens of thousands Much faster; no per-mark DOM, so no per-element events
WebGL Pixels drawn by the GPU Hundreds of thousands to millions Fastest; limited trace types, and browsers cap the number of WebGL contexts per page

The numbers are rough guides; your hardware and chart type matter.

Python
import plotly.graph_objects as go
import plotly.express as px
from bokeh.plotting import figure

# Plotly: WebGL traces
fig = go.Figure(go.Scattergl(x=df["x"], y=df["y"], mode="markers"))
fig2 = px.scatter(df, x="x", y="y", render_mode="webgl")

# Bokeh: choose the output backend
p = figure(output_backend="webgl")

Read-only. Plotly Express picks WebGL automatically for scatter plots with more than about a thousand points; the explicit forms are useful when you want control. Altair renders through Vega, which can draw to canvas or SVG; canvas is the usual default in the embedding tools.

WebGL has costs. A page with many WebGL charts can hit the browser's limit on simultaneous contexts, and a few styling options are unavailable on WebGL traces. Use it for the one big chart, not for a wall of small ones.

Downsampling: keep the shape, drop the points

A line chart 1,000 pixels wide cannot show 100,000 distinct x-positions. Extra points land on the same pixel column and add cost without detail. Downsampling keeps a few hundred points that look the same.

The naive way is to keep every n-th point. It is fast and often wrong: if a spike falls between two kept points, it disappears. Better algorithms choose which point to keep from each bucket. LTTB (Largest Triangle Three Buckets) splits the series into buckets, and in each one picks the point that forms the largest triangle with the previously chosen point and the average of the next bucket. Peaks and dips form big triangles, so they survive.

The program below builds a 100,000-point series with one hidden spike, then downsamples it two ways.

JS Stride versus LTTB downsampling

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

No output.

Both versions keep 500 points, a 200-fold reduction, and the path text shrinks from about a million characters to about five thousand. Only LTTB still shows the spike at 80.3; the every-n-th version reports 22.0, so a chart built from it would hide the one event you probably care about. The first and last points are always kept so the line spans the same range.

Downsampling has limits you should state. It is for viewing a line; do not compute statistics from the sampled points. In an interactive chart, downsample for the zoomed-out view and fetch finer data when the reader zooms in (Plotly's relayout event and Bokeh server callbacks both provide the range, see Plotly.js in the Browser and Bokeh Server and Streaming Data). The plotly-resampler package is a ready-made implementation of this idea for Plotly.

Aggregation: bin the points

A scatter plot of two million points is not a million-point problem; it is a density problem. Dots overlap until the middle is a solid blob and the counts are invisible. The answer is to bin the plane into a grid, count how many points fall into each cell, and color the cells. The data sent to the browser shrinks from millions of points to a few thousand cells, and the picture is more honest.

Datashader is the Python library built around this idea. It aggregates points onto a pixel grid (the "canvas"), maps the counts to colors (the "shading") and gives you an image or array. You can show that in Bokeh or Plotly as an image, or through HoloViews, which re-aggregates when you zoom. The same effect is available without extra libraries: numpy.histogram2d or pandas cut plus groupby produce the counts, and Plotly's go.Heatmap or Altair's mark_rect with binned encodings draws them.

JS Bin 200,000 points into a density grid

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

No output.

Two clusters appear clearly: a bigger one on the left (70% of the points) and a smaller one on the right. A real scatter plot would show one solid smear. Two details are worth copying. First, the counts are shaded on a logarithmic scale, because the busiest cell holds thousands of points and the faint edges hold a handful; on a linear scale the edges would vanish. Second, the output is only 480 numbers, not 200,000 points. Whatever you draw it with, the browser's work is now fixed by the grid size, not by the data size.

Do the work on the server

Where the reduction happens matters as much as how:

  • In Python, before plotting. Group, resample or bin with pandas, NumPy or a database query (GROUP BY in SQL does much of this for free), then pass the small result to the chart. This is the simplest and best option for static reports. See Preparing Data: Tidy Format and Reshaping.
  • In a server-backed app. A Dash or Bokeh server callback receives the current zoom range and returns data for that range only. Memory use in the browser stays constant however big the table is.
  • In the specification. Altair raises an error above 5,000 rows by default, to stop you from embedding a huge table. Your options are to aggregate first, to switch to a data transformer that keeps the data out of the HTML (a JSON file reference, or VegaFusion, which runs aggregation in Python), or to remove the limit and accept the file size. Raising the limit is the least useful choice.

Measure, do not guess

Performance advice goes stale as browsers change, so build the habit of measuring. The program below times how long it takes just to write the path text for a line of different sizes. That is only the first step of drawing (the browser still has to parse and rasterize the path), but it shows how the cost scales. The exact milliseconds depend on your device and will differ from run to run; look at the ratios.

JS How the cost grows with the number of points

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

No output.

Expect roughly ten times more points to cost roughly ten times more time, and that is just string building. In a real chart the draw step adds to it on every zoom or hover. Cutting rows from 500,000 to 500 changes the work by a factor of a thousand.

What is real and what is the stand-in

  • Real, read-only: Scattergl, render_mode, output_backend, Datashader and the Altair data transformers. They need Python and a browser display.
  • Stand-in: the LTTB function is a faithful implementation of the algorithm, and the binning code shows the idea of density aggregation. They do not draw anything, and the timing measures string building, not rendering.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is wrong with downsampling a line by keeping every n-th point?

    Stride sampling ignores the values, so peaks and dips between kept points are lost. LTTB chooses points by the area of the triangles they form, so extremes survive.
  2. 2. A scatter plot of two million points is one dense blob. Which approach fits best?

    Overplotting hides density. Aggregating into cells shows where the points concentrate and shrinks the data sent to the browser.
  3. 3. Which is the most scalable way to draw many markers in the browser?

    WebGL draws on the GPU and handles hundreds of thousands of marks. SVG slows down with many elements. The catch is limited trace types and a cap on WebGL contexts per page.
  4. 4. Why shade density counts on a logarithmic scale?

    Counts vary by orders of magnitude. A log scale keeps both the dense core and the sparse edges visible.

Summary

  • Large charts are slow because of data size, one mark per row, and redraw cost; reduce rows first.
  • SVG suits thousands of marks, canvas tens of thousands, WebGL hundreds of thousands and more, with limits on traces and contexts.
  • Downsample lines with an algorithm such as LTTB so peaks survive; never compute statistics from the sample.
  • Aggregate scatter data into bins (the Datashader idea) and shade by log counts.
  • Do the reduction in Python or in a server callback, not in the browser; avoid lifting Altair's row limit.
  • Measure with real data, and read ratios instead of exact milliseconds.

Next: Exporting, Embedding and Sharing Charts.