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
