Line charts and sparklines

Intermediate

Introduction

When the question is "how did it change?", the answer is a line chart. Joining points in time order lets the eye follow the trend, see where it speeds up, and spot the month that broke the pattern. This lesson draws line charts as text, which makes the mapping from values to positions explicit, and introduces the sparkline, a chart small enough to sit inside a sentence.

How it works

A time series goes left to right, one column per time step, with values mapped to height. On a character grid with height rows, the row for a value is

row = int((v - lo) / (hi - lo) * (height - 1) + 0.5)

where lo and hi are the smallest and largest values. Drawing the grid from the top row down, a * marks each point's row in its column. The vertical axis of a line chart does not have to start at zero, unlike a bar chart, because the line shows change rather than size; but it should be labelled so the reader knows the range.

Keep time intervals even: if a few months are missing, leave gaps rather than squeezing the remaining points together, which would fake a steeper slope. Draw markers on the points when there are few of them, and leave them off when there are many.

For several series, use a shared scale: compute lo and hi over every series, or the lines cannot be compared. More than four or five lines become spaghetti; then split them into small multiples (a later lesson) or highlight one line and grey the rest. Label lines directly at their ends when possible, rather than making the reader match colours to a legend.

A sparkline, named by Edward Tufte, drops the axes entirely: it maps each value to one of eight block characters, ▁▂▃▄▅▆▇█, so a whole trend fits in a word. Sparklines suit tables and dashboards, where they show the shape next to the latest number.

The aspect ratio changes the story. The same data looks flat in a wide, short chart and dramatic in a narrow, tall one. A common guideline is to choose the shape so that the important slopes are near 45 degrees.

In Matplotlib, plt.plot(days, visits, marker="o") draws a line with markers, and calling plt.plot once per series with a label= and then plt.legend() draws several series on one scale.

Example

# Line charts as text: a sparkline in one line, and a small grid with time across.
visits = [12, 15, 14, 20, 26, 24, 31, 29, 35, 40]
BLOCKS = "▁▂▃▄▅▆▇█"


def sparkline(values):
    lo, hi = min(values), max(values)
    return "".join(BLOCKS[int((v - lo) / (hi - lo) * 7 + 0.5)] for v in values)


def line_grid(values, height):
    lo, hi = min(values), max(values)
    rows = [int((v - lo) / (hi - lo) * (height - 1) + 0.5) for v in values]
    for level in range(height - 1, -1, -1):
        label = hi if level == height - 1 else lo if level == 0 else ""
        marks = "".join("*" if r == level else "." for r in rows)
        print(f"{label:>3} |{marks}")
    print("    +" + "-" * len(values))


print("visits:", sparkline(visits))
line_grid(visits, 6)
Open in compiler

Line by line

  1. sparkline maps each value to one of eight blocks, from the smallest value (▁) to the largest (█).
  2. line_grid computes a row for every value, then prints the rows from the top level down.
  3. The top row is labelled with the largest value and the bottom row with the smallest.
  4. The last line is the time axis, one - per point.

Expected result

visits: ▁▂▂▃▅▄▆▅▇█
 40 |.........*
    |........*.
    |....*.**..
    |.....*....
    |.*.*......
 12 |*.*.......
    +----------

In the real world

Stock charts, weather records, website analytics and health trackers are line charts. Financial tables often put a sparkline beside each company's price, and status pages show a sparkline of response times per service. Editors choose the aspect ratio deliberately, because a flattened or stretched chart can mislead.

Common mistakes

  • Uneven time steps drawn as even, which distorts slopes.
  • Separate scales for series in one chart, which makes them look comparable when they are not.
  • Too many lines in one chart, which nobody can follow.
  • Lines across unordered categories, which invent a trend.

Best practices

  • Keep time on the horizontal axis with even spacing, and leave gaps for missing data.
  • Use one shared scale and label lines directly.
  • Use sparklines in tables and dashboards to show trends compactly.

Summary

Line charts show change over time by mapping values to heights in time order, with any axis start as long as it is labelled. Several series share one scale and are labelled directly; sparklines show a trend in one word; and the aspect ratio should keep important slopes near 45 degrees.

Quick check

1. Why may a line chart's axis start above zero?
2. What is a sparkline?
3. Two series are drawn with separate scales in one chart. What goes wrong?
4. How does a tall, narrow chart change a trend?

Key takeaways

  • Put time on the horizontal axis and keep the intervals even.
  • Several series need one shared scale and clear labels.
  • Sparklines show the shape of a trend in the space of a word.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.