Python chart API and figure model
The Python chart API lets an application describe a chart as a declarative tree. A public Chart combines marks, axes, annotations, presentation, and behavior. XY then builds one internal figure model that can drive a live widget, a standalone HTML document, or a static export.
Declarative chart grammar
The chart grammar separates what is shown from how the chart is presented and how it behaves. A chart container owns the composition, marks provide data-driven geometry, and supporting nodes describe scales, chrome, interaction, and output policy.
flowchart LR
A[Chart container] --> B[Marks]
A --> C[Axes and scales]
A --> D[Annotations and chrome]
A --> E[Interaction and export policy]
B --> F[Internal figure]
C --> F
D --> F
E --> F
F --> G[Live widget]
F --> H[Standalone HTML]
F --> I[Static output]
Chart composition
xy.chart(...) is the neutral single-panel container for mixed marks and overlays. Family containers such as xy.line_chart(...) and xy.scatter_chart(...) express a clearer intent when the children belong to one family, but they return the same public Chart interface. facet_chart(...) is the separate composition form for repeating a chart over groups. python/…/components.py• L6506-6518, python/…/components.py• L5364-5412
Chart-level data= is the default source for marks that use named columns. A mark can provide its own data= when it needs a different source. Declaration order controls the layer order, so a later mark paints over earlier marks. python/…/components.py• L3575-3589, python/…/components.py• L3962-3984
References: python/…/components.py• L3575-3589 python/…/components.py• L3638-3773 python/…/components.py• L3962-3984 python/…/components.py• L6506-6518
Mark construction
A Mark is a declarative description of one visual series. It names a mark kind, binds position values or columns, carries optional channels such as color and size, and keeps mark-specific display choices with that series. The public factories create these descriptions; application code does not need to create a renderer object.
The fluent Figure methods and the declarative composition API use the same mark functions. This keeps signatures, defaults, channel handling, and validation aligned across the two Python entry points. A high-level mark can produce more than one internal trace when its visual form needs separate geometry, but it remains one chart-level declaration. python/…/marks.py• L1-9, python/…/_figure.py• L607-632
The build step resolves each mark's data and axis IDs, then sends it to the shared mark function for that kind. Mark functions normalize values, resolve channels, and add trace data to the figure. They do not create renderer objects. python/…/components.py• L182-199, python/…/components.py• L3962-3984, python/…/marks.py• L1348-1595
Mark families express different visual questions without changing the chart grammar:
| Mark family | Main question |
|---|---|
| Points and lines | Where are observations and how do they change? |
| Filled regions and intervals | What span or uncertainty surrounds a value? |
| Rectangles and cells | How do counts, categories, or grids compare? |
| Segments and meshes | What edges, shapes, or connected geometry matter? |
| Flow bands and stages | How does quantity move between ordered positions? |
See chart families and layout for the chart catalog and the way related marks form larger chart types.
References: python/…/marks.py• L1-9 python/…/marks.py• L285-427 python/…/marks.py• L1348-1595 python/…/_figure.py• L607-632
Axes and chart chrome
Axes define the named coordinate systems that marks use. They describe scale choice, domain, direction, ticks, labels, and axis-side presentation. A mark that names a secondary axis must have a matching axis node, which makes a second scale an explicit chart decision rather than an invisible mark setting. python/…/components.py• L219-263, python/…/components.py• L2652-2778, spec/…/chart-grammar.md• L37-67
Chart chrome reads the chart model without becoming a second data owner. Legends summarize mark channels and names, tooltips provide hover readouts, colorbars explain continuous color, and modebars expose permitted navigation controls. Annotations such as rules, bands, labels, arrows, and callouts are coordinate-aware overlays, not data traces. python/…/components.py• L202-217, python/…/components.py• L265-327, python/…/components.py• L4006-4116
The detailed presentation rules belong to styling, themes, and annotations. Interaction policy belongs to interaction and view state.
References: python/…/components.py• L202-217 python/…/components.py• L219-263 python/…/components.py• L265-327 python/…/components.py• L4006-4116
Figure lifecycle
The public chart object is a plan until an output or figure access method needs a built result. The lifecycle turns that plan into one internal figure, retains it for later use, and keeps data mutation separate from structural composition.
Chart build
Chart.figure() is the main build boundary. It checks child types and axis settings, creates the internal figure, applies chart configuration, resolves mark data, builds traces, applies annotations and chrome, validates interaction settings, and caches the result. A chart with a bad child, unknown mark, invalid axis reference, or unsupported coordinate choice fails at this boundary before a renderer receives it. python/…/components.py• L3775-3816, python/…/components.py• L3818-4011, python/…/components.py• L4113-4118
The build sequence is:
- Validate the composition and axis nodes.
- Create the internal figure with dimensions, labels, and coordinate mode.
- Apply axes and chart-level policy.
- Build marks against shared or mark-level data.
- Add annotations and presentation nodes.
- Validate the finished figure and retain it for output.
This order matters because marks need axis assignments and chart settings before they become traces. It also lets the completed figure resolve ranges and output metadata from all of its marks together.
References: python/…/components.py• L3775-3816 python/…/components.py• L3818-4011 python/…/components.py• L4113-4118
Cached figure and output handles
The internal Figure is a cached engine object, not the main authoring surface. widget(), show(), to_html(), and the image methods reuse the same built model, so changing the output target does not require rebuilding the composition. chart.figure() exposes that model as an advanced access point for code that needs figure-level operations. python/…/components.py• L3775-3780, python/…/components.py• L4146-4211, python/…/components.py• L4251-4314, docs/…/figure-methods.md• L8-23
The output handle determines the host boundary. A live widget keeps a connection for callbacks and programmatic updates. Standalone HTML keeps the chart interactive without a Python process. Static methods create bytes or files from the same chart meaning. See export and static rendering and browser rendering runtime for output-specific limits and client-side behavior.
References: python/…/components.py• L3775-3780 python/…/components.py• L4146-4211 python/…/components.py• L4251-4314 python/…/_figure.py• L2233-2329
Data mutation and view state
The chart structure is fixed after build, while supported live traces can receive new data. append() extends a trace without adding a new mark or changing the chart grammar. Exact picks and selections use the figure's source data, while the browser can keep its current view and request a new representation as the data changes. python/…/components.py• L4463-4491, python/…/_figure.py• L2023-2054
View state is also separate from chart structure. Ranges and geometric selection can be read or changed through the live chart path without rebuilding marks. The kernel-side view cache is an observed mirror of the browser state, so a read can briefly lag a command that the browser has not yet committed. Interaction and view state explains the event and state contract.
References: python/…/components.py• L4213-4241 python/…/components.py• L4463-4491 python/…/_figure.py• L1997-2054 python/…/_figure.py• L2205-2231
Configuration and validation
Configuration nodes give a chart one place to describe appearance, behavior, scale policy, animation, and export defaults. Validation keeps these descriptions finite, compatible, and meaningful before they cross into a renderer or host.
Configuration groups
Chart configuration is grouped by the concern it controls. This keeps a mark's data meaning separate from the chart's presentation and host behavior.
| Configuration group | Main role | Examples |
|---|---|---|
| Layout | Set the chart's space and frame | width, height, padding, title |
| Coordinate policy | Set scale meaning and axis limits | x_axis, y_axis, theta_axis, r_axis, coords |
| Presentation | Set shared visual language and chrome | theme, legend, tooltip, colorbar, modebar |
| Interaction | Set local gestures, events, and linked ranges | interaction_config, on_hover, on_select, on_view_change |
| Motion and output | Set transitions and export defaults | animation, export_config |
Singleton nodes use the last node of their group. Theme and interaction nodes merge in declaration order, so later explicit values replace only overlapping settings. Direct chart properties and callbacks form the chart-level policy that is applied with these nodes. python/…/components.py• L3884-3958, python/…/components.py• L4013-4116, python/…/components.py• L3335-3472
References: python/…/components.py• L323-345 python/…/components.py• L348-366 python/…/components.py• L3884-3958 python/…/components.py• L4013-4116
Validation boundary
Validation occurs at more than one useful boundary. Factory calls check the shape and type of individual configuration nodes. Figure build checks relationships between children, marks, and axes. Payload construction checks the finished coordinate system and produces only a renderer-ready description. python/…/components.py• L2652-2778, python/…/_figure.py• L887-1105, python/…/_payload.py• L312-434
XY rejects a configuration that could draw a plausible but wrong chart. Examples include an unknown axis ID, an invalid scale setting, a mark unsupported by the selected coordinate system, or a secondary axis without an axis declaration. This makes validation a product boundary: callers get a clear correction instead of a silently altered picture.
Structural probing is the data-free validation path. Under xy.structural_probe(), a mark with empty data validates its configuration and contributes no trace. This allows a host or compile step to check chart structure without inventing data, while normal builds keep their usual non-empty data requirements. python/…/_figure.py• L54-72, python/…/marks.py• L2013-2023, python/…/marks.py• L2424-2436
References: python/…/components.py• L2652-2778 python/…/_figure.py• L887-1105 python/…/_figure.py• L54-72 python/…/_payload.py• L312-434 python/…/marks.py• L2013-2023 python/…/marks.py• L2424-2436
Serialization boundaries
Serialization converts the internal figure into a renderer-neutral chart description. The description carries chart structure, axes, trace roles, styles, interaction policy, and references to typed data buffers. Numeric source data stays outside the JSON-like description, which keeps the specification small and lets live and static hosts use the same meaning.
Figure payload
Payload construction resolves the figure's ranges, emits each trace according to its mark kind, and pairs the resulting metadata with binary columns. Large lines can use a screen-sized reduction and dense scatter can use a density representation, but the payload records that choice so downstream systems do not confuse a reduced view with the source rows. python/…/_payload.py• L257-287, python/…/_payload.py• L312-352, python/…/_payload.py• L465-492
The boundary has two owners. Python owns source values, validation, ranges, and representation decisions. The browser or static renderer owns buffer use, screen layout, and drawing. This is why the same figure can support local browser navigation while still using Python for exact source-row readout. See data ingestion and payload construction for the full data path and level of detail, density, and streaming for representation changes.
References: python/…/_figure.py• L1-7 python/…/_payload.py• L257-287 python/…/_payload.py• L312-352 python/…/_payload.py• L465-492
Host and renderer boundary
The chart model is shared across notebook widgets, application adapters, standalone HTML, and native static writers. A host may add its own chrome objects through an adapter boundary, but core XY does not serialize those opaque framework objects. The chart's own built-in chrome remains part of the portable chart description. python/…/components.py• L4122-4144, docs/…/figure-methods.md• L109-118
Standalone HTML embeds the chart description and its buffers so it can run without a kernel. Static renderers consume the same payload model with output-specific fidelity rules. A previously written HTML or image file is a snapshot; later chart mutation affects a live figure or a new export, not an old file. python/…/_figure.py• L2259-2329, python/…/components.py• L4469-4487
References: python/…/components.py• L4122-4144 python/…/_figure.py• L2259-2329 python/…/components.py• L4469-4487 docs/…/figure-methods.md• L52-61
Related systems
The chart API is the entry point. These pages explain the neighboring systems that become important after an engineer understands the figure model:
- Data ingestion and payload construction explains source columns, channels, geometry, and binary transport.
- Chart families and layout explains family wrappers, facets, overlays, and layout choices.
- Styling, themes, and annotations explains visual tokens and coordinate-aware overlays.
- Interaction and view state explains gestures, callbacks, selections, and linked ranges.
- Level of detail, density, and streaming explains screen-sized representations and live append behavior.
- Export and static rendering explains HTML, image, vector, document, and batch output.
- Browser rendering runtime explains how the client consumes the figure description and manages live rendering.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep public mark factories and the shared mark functions aligned. A changed prop name or default must behave the same in declarative composition and fluent figure construction.
- Keep chart-level data inheritance aligned with mark-level data overrides. A change in name resolution can silently bind a mark to the wrong table.
- Keep declared axis IDs aligned across axis nodes, mark bindings, range calculation, interaction policies, and payload axis records.
- Keep configuration merge rules aligned between direct chart props and child nodes. A new singleton or mergeable node must have one clear precedence rule.
- Keep structural-probe validation separate from data-dependent work. Compile-time checks must not invent rows or run aggregation on empty input.
- Keep the figure payload contract aligned across live widgets, standalone HTML, native exporters, and any host adapter that forwards the chart.
- Keep the public chart lifecycle clear when adding mutation APIs. Structural changes need a new composition, while data updates must preserve trace identity and view-state meaning.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A mark names an axis ID that has no matching axis node | The chart cannot establish the mark's coordinate system | Declare the axis explicitly before using the ID on a mark |
| A chart mixes marks with incompatible coordinate rules | One mark can be shown against a misleading scale | Let build-time validation reject the composition instead of coercing it |
| A child is not a supported chart component | The chart plan cannot be compiled into the figure model | Use marks, axes, annotations, supported chrome, interaction, animation, or export nodes |
| A configuration is valid in shape but unsupported for the selected coordinates | A renderer could produce a plausible but wrong picture | Validate the completed figure and payload before rendering |
| An empty mark is used for structural compilation without probe mode | Normal data validation rejects the mark before structure can be checked | Use xy.structural_probe() for data-free configuration checks |
| A chart is mutated after an HTML or image export | The saved artifact remains unchanged while the live object moves on | Treat exported files as snapshots and export again after data changes |
| A host expects framework chrome to be present in core serialization | Opaque adapter objects cannot cross the portable chart boundary | Mount them through chrome_components() or the host adapter |
Key files
| File | Why to read it |
|---|---|
| python/…/components.py | Defines the public component grammar, chart containers, configuration nodes, build lifecycle, and chart methods. |
| python/…/marks.py | Defines the shared mark functions used by declarative charts and fluent figure construction. |
| python/…/_figure.py | Defines the internal figure state, axis policy, range model, live mutation, view state, and output delegation. |
| python/…/_trace.py | Describes the built series model shared by mark construction, interaction, and payload generation. |
| python/…/_payload.py | Defines the boundary from a built figure to renderer metadata and binary data buffers. |
| python/…/columns.py | Defines the canonical source-column contract used by ranges, readout, selection, and streaming. |
| python/…/styles.py | Defines the cross-renderer style vocabulary and validation for marks and axes. |
| python/…/__init__.py | Shows the supported Python-facing exports and the lazy public API surface. |
| docs/…/chart-factories.md | Provides the generated reference for chart containers and shared chart props. |
| docs/…/figure-methods.md | Summarizes the public chart lifecycle, output methods, readout, and mutation boundaries. |