Chuyển đến nội dung

Bài 11 · Basic Charts

Area Charts and Time Series

Plot time series in Plotly: area and stacked area charts, percent areas, filling between lines, resampling and moving averages with pandas, range sliders and range selector buttons.

An area chart is a line chart with the space under the line filled in. The fill gives the eye a sense of volume, which is why area charts suit quantities that accumulate or add up: visitors, revenue, energy use. Stacked areas show how several series add up to a total and how the mix changes over time.

This lesson also collects the everyday tools for time series: preparing dates, smoothing with pandas, and the interactive range slider and range selector buttons that make long series easy to explore.

Area charts

An area trace is a scatter trace with a fill. px.area creates it, and color splits the data into stacked areas:

Python Stacked and percent areas

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.

Each continent became a trace in the same stackgroup, so plotly.js piles the values up instead of overlapping them. The printed fill is empty because a stackgroup already implies it: plotly.js fills the first trace down to zero and every later one up to the trace below, which is how a stack is drawn. groupnorm="percent" rescales every x position to 100%, so you see the mix instead of the total.

The same thing by hand

With graph objects you set stackgroup and fill yourself. A chart in plotly.js:

Stacked areas with plotly.js

Without stackgroup the areas would overlap and hide each other; with fill: "tozeroy" on each trace you can instead draw overlapping translucent areas (set opacity or fillcolor with an alpha value). Another useful fill is fill: "tonexty" between two lines, which shades a band such as a confidence interval.

Preparing time series with pandas

Before plotting a time series, make sure the date column is a real datetime, sorted, and (if needed) aggregated to the right resolution. Pandas does all of it:

Python Resample and smooth a time series

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.

Plotting the raw data in a light color and the smoothed trend in a strong color is the standard way to show both noise and signal. resample("W") aggregates days into weeks (use "ME" for month ends, "QE" for quarters). The first six values of a 7-day rolling mean are missing, which Plotly draws as a gap at the start.

Range slider and range selector

Time series are often long, and the interesting part may be a small window. Two built-in tools help. The range slider is a mini-overview below the chart with draggable handles. The range selector is a set of buttons ("1m", "6m", "YTD", "all") that jump to a window.

Range slider and selector buttons

In Python the same options are fig.update_xaxes(rangeslider_visible=True, rangeselector=dict(buttons=[...])). A detailed treatment, including hiding weekends with rangebreaks, is in Axes, Ticks and Scales.

Tips for time series charts

  • Use a line for continuous values and bars for totals per period (per month, per week).
  • Use markers only when points are few. Hundreds of markers turn a line into noise.
  • Keep the same y scale when you compare periods, or label clearly if it changes.
  • Add annotations for events (a launch, an outage) to explain bumps; the annotations lesson covers this.
  • For finance, candlestick and OHLC charts show open, high, low and close.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What property makes several area traces pile up instead of overlap?

    Traces that share a stackgroup are stacked on top of each other. (barmode is for bars.)
  2. 2. What does groupnorm="percent" do for stacked areas?

    You see each series' share instead of absolute values.
  3. 3. Which pandas method turns daily data into weekly totals?

    resample groups a datetime index into periods and aggregates them.
  4. 4. What is the range selector?

    The mini-chart with handles is the range slider; the selector is the button row.
  5. 5. Why plot raw data in light grey and a moving average in a strong color?

    De-emphasizing the noisy series keeps context without hiding the signal.

Summary

  • Area charts are scatter traces with fill; px.area and stackgroup stack them, groupnorm="percent" shows shares.
  • Prepare time series with pd.to_datetime, sorting, resample and rolling.
  • Raw data plus a smoothed line shows noise and trend together.
  • rangeslider and rangeselector make long series explorable.

Next: Sunburst, Treemap and Icicle Charts

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.