Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 31 ยท Getting Started

How a Chart Works Inside

See how every chart turns data into marks, scales, axes and visual encodings, and build a small SVG bar chart and scatter plot by hand.

Plotly, Bokeh and Altair look very different in code, but every chart they draw is built from the same few parts. Learn those parts once and you can read any chart library's documentation, debug a strange axis, and understand why a tooltip shows what it shows.

This lesson takes a chart apart. You will meet marks, channels, scales and axes, then rebuild a bar chart and a scatter plot from scratch in a few dozen lines of JavaScript and SVG. The real libraries do much more, but at the center they do exactly this.

From data to marks

A chart starts with a table. Each row is one observation, each column one variable:

month units region
Jan 120 North
Feb 150 North
Mar 90 South

The chart turns each row into a mark: a visible shape such as a bar, a point, a line segment or an area. The properties of a mark (where it sits, how big it is, what color it has) are called visual channels. The art of charting is deciding which column feeds which channel. This assignment is called an encoding.

Some channels are more accurate for the eye than others. Position along a common axis is read most accurately, then length, then angle and area, and color shade last. That is why a bar chart beats a pie chart for comparing values: bars use position and length, pies use angles.

flowchart LR
    D["Table<br/>(rows and columns)"] --> E["Encoding<br/>column to channel"]
    E --> S["Scales<br/>value to pixel or color"]
    S --> M["Marks<br/>bars, points, lines"]
    S --> A["Axes and legends<br/>ticks and labels"]
    M --> P["Picture"]
    A --> P

Every chart is built the same way: the scales are shared by the marks and by the axes, which is why the two always agree.

Scales: the heart of the chart

A scale is a function that maps a data value to a visual value. Three kinds cover most charts:

  • A linear scale maps a continuous range of numbers to another range, for example the units 0 to 200 onto pixels 200 down to 0. Notice the flip: in screen coordinates, y grows downward, so the largest value gets the smallest pixel.
  • A band (or ordinal position) scale gives each category its own slot. It also reports a bandwidth, the width of one slot after padding.
  • A color scale maps numbers to a color gradient (sequential or diverging) or categories to distinct hues.

Each scale has a domain (the data values it accepts) and a range (the visual values it returns). A scale can also run backward: the inverse maps pixels back to data. That inverse is how a tooltip, a zoom box or a click knows which data it points at.

JS Build a linear scale, a band scale and nice ticks

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

No output.

The maximum value in the data is 180, but the axis ends at 200. Charting libraries round the axis up to a nice number so the ticks fall on round values (0, 50, 100, 150, 200) instead of on 0, 36, 72 and so on. That is why the top of the axis is rarely exactly your biggest value. And invert(50) returns 150: the pixel row 50 corresponds to the data value 150, which is what a hover tooltip would report.

Axes and legends

An axis draws a scale: a line, tick marks at chosen domain values, and a label for each tick. A legend does the same job for non-position channels, such as color or size. Axes are generated from the scale, never drawn separately, so the bar for 90 and the tick for 90 always agree. When a chart looks wrong, the cause is often a scale: a domain that does not start at zero, a log scale you forgot, or two charts that use different domains and so cannot be compared.

A bar chart from scratch

Now put it together. The chart below uses the three pieces from the previous example, draws an SVG <rect> per row, adds gridlines and tick labels from the y scale, and encodes a second idea in color: bars above the average are dark, the rest are light. Hover a bar in your browser to see its <title> tooltip, the simplest possible interaction.

A bar chart in plain SVG

Count the ideas: one table, two scales, one mark per row, one axis from a scale, one extra channel (color). Plotly's px.bar, Bokeh's vbar and Altair's mark_bar all do this, with many more options for the same steps.

Encoding more channels: a scatter plot

A scatter plot shows how many channels one mark can carry. Each point has an x and y position, a size, and a color. Size is best mapped so that area (not radius) follows the value, which means the radius scale is a square root. A categorical column feeds the color through a lookup table. Try changing the data or the palette.

Four channels on one mark

The same table drives all four channels: price to x, rating to y, sales to size, type to color. In Altair this would be a single encode(x="price", y="rating", size="sales", color="type"); in Plotly Express, px.scatter(df, x="price", y="rating", size="sales", color="type"). Both are read-only here, but now you know what happens when you run them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a scale do?

    A scale has a domain of data values and a range of visual values. Marks and axes both use it, so they always agree.
  2. 2. A bar chart's y scale maps 0 to pixel 200 and 200 to pixel 0. Why is the range flipped?

    In SVG and on screens the origin is the top-left corner, so a bigger value must map to a smaller y pixel to appear higher.
  3. 3. How should a size channel usually be scaled for circles?

    People read circle area. If radius is proportional to the value, doubling the value makes the circle four times as large and exaggerates the difference.
  4. 4. What is an inverse scale used for?

    Interaction starts with pixels (the mouse position). The inverse scale turns them back into data values.

Summary

  • A chart turns each data row into a mark and feeds columns into visual channels such as position, length, color and size.
  • Scales (linear, band, color) map a domain of data to a range of visual values and have an inverse for interaction.
  • Axes and legends are generated from scales, so marks and labels stay consistent; axes end on nice round ticks.
  • Screen y grows downward, which is why linear y ranges are flipped.
  • Plotly, Bokeh and Altair automate exactly these steps; the SVG stand-ins here do them by hand.

Next: Choosing the Right Chart.