liveBook · Manning

Base chart

As explained in Data Storytelling with Altair and AI

A base chart is a chart specification containing shared encodings and properties from which related charts are constructed.

A base chart is a reusable Altair chart specification that holds the parts related charts share: a data source, encodings, and properties. Build it once, then derive charts by adding marks or chart-specific encodings, filters, and styling. This keeps related views consistent and avoids repeating setup code. section 6.3.3 section 3.2.4

Label a line chart without a legend

For monthly pizza and spaghetti orders, define x as Month, y as Number of Orders, color as Meal Type, and the shared dimensions in a base chart—without a mark. Derive one view with mark_line(). Derive another with mark_text(), filter it to December, and use it to place each meal label at the line’s endpoint. Layer the two results. section 6.3.3

Listing 6.12 How to build the base chart of a line chart
base = alt.Chart(df).encode(
    x=alt.X('Month',
            axis=alt.Axis(title=None,
                            labelAngle=0,
            ),
            sort=months
    ),
    y=alt.Y('Number of Orders'),
    color=alt.Color('Meal Type',scale=alt.Scale(range=['#81c01e','gray']),legend=None)
).properties(
    width=600,
    height=300
)

This specification isolates the shared data, position and color encodings, axis settings, sorting, and dimensions before any visual mark is chosen. section 6.3.3

Use a base chart when several views need a common visual foundation:

  • Create bar and line charts from identical x and y encodings, dimensions, and data; give each derived chart its own mark, color, or title before layering or concatenating it. section 3.2.4
  • Overlay a focused line on a main line chart by deriving both from shared encodings, then filtering the second view to the selected category and years. section 8.7.1
  • Build coordinated text and image call-to-action views from shared x encoding and title properties, then add each view’s mark and channel. section 9.2.2
  • Reuse a text-mark base chart for an interactive article title and headline, keeping color and selection filtering common while changing the calculated text field. section 9.2.6

Start by asking what must stay identical across the related views. Put only that common structure in the base chart. Add details afterward: a mark for how to draw each view, a filter for a subset, or a channel used by only one view. A base chart need not always be mark-free; when related views share a text mark, the common mark can belong in their base specification. section 6.3.3 section 9.2.6

How Data Storytelling with Altair and AI covers it

How deep it goes
Demonstrates reusable specifications through line labels, focused overlays, text-image views, and interactive text; does not describe underlying rendering internals.
Examples
Uses meal-order line labels, a salmon-focused overlay, call-to-action text and images, article titles and headlines, and bar-line compound charts.
What it assumes
Assumes practical Altair chart construction while showing shared data, encodings, properties, marks, and filters in examples.
What you can do after
Apply it via Altair to derive consistent chart layers or companion views from a shared specification.
How it connects
Connects Altair encodings, marks, transform_filter(), layering, concatenation, repeated charts, selections, and calculated fields.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.