Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 31 ยท Projects

Project: Trend Explorer with Linked Views

Build a trend explorer: smooth noisy time series with rolling averages, draw small multiples, and link a brush on the time axis to a summary chart.

Public dashboards about health, traffic, energy or sales all share one problem: a time series that is noisy. Daily numbers jump around, regions have different scales, and the reader wants to ask "what happened between these two dates?" In this project you build a small trend explorer that handles all three: a rolling average to calm the noise, small multiples to compare regions, and a brush (a selected time range) that is linked to a second view.

We use made-up weekly numbers for three regions, so there is nothing to download and no real-world claim to defend. The real libraries (Plotly, Bokeh, Altair) cannot run on this site; their code is read-only, and the runnable parts are JavaScript stand-ins that do the same work in a few lines. If you have not seen selections yet, read selections and interactive Altair charts and Bokeh tools first.

Step 1: smooth the noise with a rolling average

A rolling average (also called a moving average) replaces each point by the mean of the last n points. Spikes shrink, trends stay. Two decisions matter:

  • Window size. A 7-point window on daily data removes weekday effects; a 28-point window shows slower trends but lags behind real changes.
  • Trailing or centered. A trailing window uses only past values, so it is the honest choice for monitoring (you cannot know the future). A centered window lines up better with turning points but needs future data.

The first size - 1 points have no full window. Show them as gaps, not as zeros, otherwise the line plunges at the start.

The program below creates three weekly series with a slow wave, a regional offset and a little deterministic jitter. It then smooths each with a 7-week trailing window and compares how bumpy the lines are by measuring the average absolute week-to-week change.

JS Rolling average on three series

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

No output.

The dashes show the six points without a full window. The roughness numbers fell for every region, by roughly a fifth to a third: that is the smoothing. Change 7 to 3 or 15 and watch the trade-off between calm and lag.

In the real libraries you rarely write this loop. In pandas it is one call, and Altair can do it inside the chart:

Python
df["smooth"] = df.groupby("region")["cases"].transform(
    lambda s: s.rolling(7).mean()
)

# Altair: a window transform, computed in the browser
alt.Chart(df).mark_line().encode(
    x="week:Q", y="smooth:Q", color="region:N"
).transform_window(smooth="mean(cases)", frame=[-6, 0], groupby=["region"])

Both leave the first six values empty (NaN in pandas), which Plotly, Bokeh and Altair draw as gaps. The window frame [-6, 0] means "six rows back through the current one".

Step 2: small multiples

Three lines in one plot are fine; thirty are a tangle. Small multiples (also called facets or trellis charts) repeat the same chart once per group, with the same axes, so the eye compares shapes instead of decoding colors. The one rule that makes them honest is shared scales: if every panel has its own y axis, a tiny change in one region can look as dramatic as a huge one in another.

flowchart LR
    D["Long table<br/>week, region, value"] --> S["Split by region"]
    S --> P1["Panel: North"]
    S --> P2["Panel: South"]
    S --> P3["Panel: West"]
    SC["One shared<br/>x and y scale"] --> P1
    SC --> P2
    SC --> P3

Splitting by group and sharing one pair of scales is all a facet does.

In Altair you write .facet(row="region") or column=; in Plotly Express px.line(df, x="week", y="value", facet_row="region"); in Bokeh you build a gridplot with shared ranges. The composition lesson covers faceting in Altair.

A brush is a rectangle or time range the reader drags on a chart. Two things happen: the chart marks which points are selected, and other views react. Linked views are the reason interactive charts beat static images: the reader asks a question by dragging, and the answer appears in a second chart.

In Altair the pieces are alt.selection_interval(encodings=["x"]) for the brush, add_params to attach it to a chart, and transform_filter on a second chart to react. In Bokeh a BoxSelectTool on one figure changes the selection of a shared ColumnDataSource, and every glyph on that source updates. Both are read-only here.

The page below is the stand-in. It has three small multiples with a shared y scale and the raw series in grey with the 7-week average on top. A pair of range sliders is the brush: they select a start week and an end week, shown as a shaded band in every panel. The bars underneath show the mean of each region inside the brush. Drag the sliders and watch all four views change together.

Small multiples with a linked brush

Notice what the design buys you. The grey raw line and the blue smoothed line sit in the same panel, so the reader sees both the data and the summary of it. The y axes are identical, so the vertical position of a line means the same thing in every panel. And the bars are always computed from the brushed range, never from the whole series, which is exactly what transform_filter(brush) does in Altair.

Swap the sliders (put "from" after "to") and the code swaps them back: a good interaction never breaks on an odd gesture.

Step 4: finishing touches

A trend explorer earns trust through details:

  • Say what is smoothed. The legend or a caption must state "7-week average"; hiding the smoothing is a form of misleading the reader.
  • Keep the raw data visible, lightly, behind the average.
  • Label the brush. The readout line states the exact range and length, so the shaded band is never ambiguous.
  • Use a common y baseline that does not exaggerate. Here 60 to 180 is shared; the pitfalls lesson discusses truncated axes.
  • Make it keyboard friendly. Range inputs work with the arrow keys; a drag-only brush does not. See color and accessibility.
  • Time zones and gaps. Real series have missing days; draw gaps rather than interpolating, and resample before smoothing, as the time series lesson shows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a trailing rolling average the safer default for monitoring dashboards?

    A centered window needs future points. A trailing window can be computed up to today. The price is some lag at turning points.
  2. 2. A 7-point rolling mean is computed on 40 points. How many points get a value?

    The first 6 points lack a full window, so 40 - 6 = 34 points have a value. Those 6 should be drawn as gaps, not zeros.
  3. 3. What makes small multiples honest when you compare panels?

    With independent scales a small change can look huge. Shared scales let the eye compare heights across panels.
  4. 4. What does a linked view do in the explorer?

    The brush is shared state. Other views filter or summarize by it, like transform_filter(brush) in Altair or a shared ColumnDataSource in Bokeh.

Summary

  • Rolling averages calm noisy series; choose the window to match the rhythm of the data and state it on the chart.
  • Trailing windows leave the first size - 1 points empty: draw them as gaps.
  • Small multiples with shared scales compare many groups without a tangle of colored lines.
  • A brush is shared state; other views filter or aggregate by it. Altair uses selection_interval, Bokeh a shared data source.
  • Keep raw data visible, label the brushed range, and make interactions work with the keyboard.

Next: Project: Stock Price Candlestick Chart