Styling, themes, and annotations
XY gives a chart one visual language for data marks, axes, browser chrome, legends, color scales, and annotations. A chart can use reusable theme tokens for broad changes, CSS-facing styles for supported surfaces, and data-linked annotations that remain in place as the view changes. This page sits below the Python chart API; see data ingestion and payloads, chart families and layout, interaction and view state, level of detail and streaming, and export and static rendering for neighboring concerns.
Visual model
Styling has two related scopes. Data marks and axes use values that render as chart geometry, while legends, labels, tooltips, and controls use browser-facing chart slots. Themes connect these scopes with shared colors and layout values.
Style tokens and themes
The xy.theme() component defines chart-wide values. Its main tokens cover the figure background, plot background, grid, axes, text, crosshair, selection, and other chart chrome. The theme can also define a categorical palette for unnamed series and categorical color channels. (See python/…/components.py• L338-346, python/…/components.py• L3406-3472.)
Theme values live on the chart root. A token can therefore be supplied by the chart or inherited from a host page. A CSS variable such as var(--brand) is resolved at the place where a chart uses it, which lets one application token drive many marks.
The broad styling relationship is:
flowchart LR
Host[Host CSS and tokens] --> Root[Chart root]
Theme[xy.theme values] --> Root
Root --> Chrome[Axes and browser chrome]
Root --> Marks[Data mark paint]
Local[Mark, axis, or component style] --> Marks
Local --> Chrome
Use the narrowest scope that expresses the intent. Use a mark or axis style for one piece of geometry. Use a component style or chart slot for one chrome surface. Use a theme token when several surfaces must change together. Chart-root styles are the most reliable place for values that must also reach static output. (See python/…/_figure.py• L176-193, python/…/_figure.py• L1910-1930, js/…/20_theme.ts• L76-102.)
References: python/…/components.py• L338-346 python/…/components.py• L3406-3472 python/…/_figure.py• L176-193 python/…/_figure.py• L1910-1930 js/…/20_theme.ts• L76-102
CSS-facing customization
XY accepts CSS-like declarations where the renderer can give them the same meaning. Mark styles use properties such as fill, stroke, stroke-width, opacity values, dashes, caps, marker shapes, and rectangular corner radius. Axis styles use a separate vocabulary for grid, axis, tick, label, font, direction, and spacing controls. Python spellings remain accepted, but the shared form is CSS kebab-case. (See python/…/styles.py• L246-315, python/…/styles.py• L398-463.)
The validation boundary protects the chart from two kinds of silent error. It rejects a property that a mark family cannot render, and it rejects unsafe or malformed CSS values. Closed color, length, and number forms are checked early. Browser-only forms such as var(), oklch(), color-mix(), and calc() can pass when their shape is safe and the browser can resolve them. (See src/css.rs• L451-485, src/css.rs• L598-649.)
Browser chrome exposes stable slots for shared CSS and utility classes. Important styling slots include the root, title, axes, annotation labels, legend parts, colorbar parts, tooltip parts, selection, crosshair, and modebar. CSS can style these DOM surfaces, but it cannot select an individual point, bar, line segment, or canvas-painted annotation shape. (See python/…/dom.py• L7-65.)
References: python/…/styles.py• L246-315 python/…/styles.py• L398-463 src/css.rs• L451-485 src/css.rs• L598-649 python/…/dom.py• L7-65
Color mapping
Color mapping turns a value or category into paint. XY keeps categorical palettes separate from continuous colormaps so that a category keeps its identity while a numeric value keeps its position on a scale.
Categorical palettes
The theme palette is a color cycle for unnamed series and a lookup for categorical color channels. A list assigns colors by position. A mapping assigns colors by category label, which keeps the same category stable across marks and facet panels even when a panel does not contain every category. Explicit mark colors take priority over the cycle. (See python/…/components.py• L3436-3451, python/…/_figure.py• L185-193.)
Palette colors must be fixed colors that the chart can resolve without a browser. This keeps the lookup the same in browser rendering, density surfaces, SVG, and native raster output. A single mark may still use a browser-resolved color expression because it does not need an indexed palette. (See python/…/components.py• L3448-3451.)
Continuous colormaps and colorbars
A continuous colormap maps a numeric range to a smooth color ramp. XY supports built-in names and custom ramps. Custom ramps use fixed color stops, including positioned stops or an equivalent CSS linear gradient, and all chart surfaces use the same interpolated ramp. A name ending in _r reverses a built-in map. (See js/…/10_colormaps.ts• L22-87.)
A colorbar explains a continuous mapping. XY can derive its domain, ramp, and label from a compatible mark, or the chart can set a title, orientation, and finite tick positions. In a layered chart, the last compatible continuous mark supplies the inferred scale. Constant colors and ordinary categorical colors do not produce a continuous colorbar. (See python/…/components.py• L3215-3250, tests/test_declarative_colorbar.py• L45-79, tests/test_declarative_colorbar.py• L193-219.)
flowchart LR
Values[Numeric values] --> Domain[Color domain]
Domain --> Ramp[Colormap ramp]
Ramp --> Marks[Colored marks]
Ramp --> Bar[Colorbar and ticks]
References: python/…/components.py• L3215-3250 python/…/components.py• L3436-3451 js/…/10_colormaps.ts• L55-87 tests/test_declarative_colorbar.py• L45-79 tests/test_declarative_colorbar.py• L193-219
Font metrics
Text size affects both appearance and layout. Axis gutters, title space, legend size, annotation placement, and static output need a predictable estimate of text width and height before the chart is drawn.
Shared text geometry
The native static path uses an embedded DejaVu Sans face and shared advance data. The same advance model lets static layout reserve space for the text it will draw. Unknown glyphs use a replacement-glyph width instead of causing the layout to fail. (See python/…/_fontmetrics.py• L1-9, python/…/_fontmetrics.py• L77-91.)
Browser charts can inherit a host font or use a loaded custom font. Static SVG and browser capture can preserve more font declarations than the native bitmap path. Native raster output uses its embedded face, so a host font choice does not change those pixels. Keep export-critical typography on the chart and choose the export path with these limits in mind; the export and static rendering page covers that choice. (See src/font.rs• L1-20, python/…/_svg.py• L1350-1377.)
References: python/…/_fontmetrics.py• L1-9 python/…/_fontmetrics.py• L77-91 src/font.rs• L1-20 python/…/_svg.py• L1350-1377
Legends
A legend explains the named series and categorical entries in a chart. It is both a visual guide and a live control surface when the browser client enables highlight or toggle behavior.
Legend entries and placement
Marks with names contribute series entries. A categorical color channel contributes one entry per category so that the legend describes the visible colors rather than only the parent trace. The legend can be hidden, titled, split into columns, anchored, or placed at a standard location. (See python/…/components.py• L3123-3173, python/…/_svg.py• L6416-6445.)
loc="best" chooses the least-occupied standard position for an unanchored Cartesian legend. The first choice uses the plotted geometry and visible annotation obstacles. The browser can refine that choice after a resize or settled view change. An explicit location, an anchor, and polar placement stay fixed. (See python/…/_legendfit.py• L1-15, python/…/_legendfit.py• L190-213.)
Legend styling and visibility
The legend container and its title, item, swatch, and label have separate browser slots. A legend can also use a component-local style. Static writers use the renderable part of the same style values, while browser classes and general host CSS need a browser surface. (See python/…/dom.py• L13-25, python/…/_svg.py• L1399-1438.)
In a live chart, clicking a legend entry can hide or show a whole trace or one category. Highlight can dim other entries. Hidden geometry is excluded from drawing, picking, selection, and large-data view work. A mark-owned label, such as a label generated for a funnel stage, follows the visibility of the geometry it describes; an author-written annotation does not. (See python/…/components.py• L3141-3150, js/…/51_annotations.ts• L596-608.)
References: python/…/components.py• L3123-3173 python/…/_legendfit.py• L1-15 python/…/_legendfit.py• L190-213 python/…/_svg.py• L6416-6445 python/…/_svg.py• L1399-1438 js/…/51_annotations.ts• L596-608
Annotations
Annotations add meaning without changing the data marks. They can mark a coordinate, identify a range, connect two points, or place text near a point. Their data anchors follow the chart's axes while text offsets use screen pixels.
Annotation types and coordinates
The public annotation set includes rules, bands, thresholds, threshold zones, text, labels, markers, arrows, and callouts. Rules and bands describe axis values. Text, markers, arrows, and callouts describe points or paths. A threshold is a semantic rule alias, and a threshold zone is a semantic band alias. (See python/…/_annotations.py• L27-190, python/…/_annotations.py• L192-388.)
Point coordinates normally use data space. Categorical axis labels can be used where the axis has categories. dx and dy are screen-space offsets, so a label can remain readable while its data anchor moves during pan or zoom. Point-anchored annotations also follow polar coordinates. Cartesian rules and bands are not approximated on polar charts. (See python/…/_annotations.py• L451-541, python/…/_figure.py• L899-915.)
Annotation layers and labels
Annotation shapes use the chart's painted annotation layer. Labels use DOM elements so they can wrap, receive classes, and respond to chart CSS. Geometry styling controls the shape. Label styling controls the text. For example, shape opacity does not automatically dim a label; label_opacity controls label fade when needed. (See js/…/51_annotations.ts• L10-39, js/…/51_annotations.ts• L727-901.)
Labels avoid common collisions within the chart. Rule and band labels can take nearby rows or columns when their default positions overlap. Callout connectors use the final label position, so the pointer stays attached after label adjustment. Static writers draw the same broad annotation concepts with their own text and shape output. (See js/…/51_annotations.ts• L963-1032, python/…/_raster.py• L1706-1820.)
References: python/…/_annotations.py• L27-190 python/…/_annotations.py• L192-388 python/…/_annotations.py• L451-570 python/…/_figure.py• L899-915 js/…/51_annotations.ts• L10-39 js/…/51_annotations.ts• L727-901 js/…/51_annotations.ts• L963-1032 python/…/_raster.py• L1706-1820
Rendering surfaces
The same chart specification can reach a live browser, SVG, native raster, PDF, or a browser-backed capture. Styling travels as far as the target surface can express it.
Browser chrome and static output
Browser chrome uses the CSS cascade. Chart tokens, component styles, slot styles, classes, host themes, and state-aware CSS can affect DOM surfaces. The browser also observes dark-mode classes and can refresh computed colors when the host theme changes. (See js/…/20_theme.ts• L95-121, js/…/20_theme.ts• L122-226.)
SVG and native raster output have no host CSS cascade. They use chart-local tokens and the static values that their writers understand. Static text and built-in legends and colorbars have explicit support. Live-only surfaces such as tooltips, modebars, crosshairs, and selections have no equivalent in a still file. (See python/…/capabilities.py• L247-296, python/…/_svg.py• L1363-1377.)
When a visual rule must agree across output types, prefer typed mark or axis values, chart-local theme tokens, and renderable component options. Use host CSS and utility classes for browser-only adaptation. The export and static rendering page describes the browser-backed and native export boundaries in more detail.
References: js/…/20_theme.ts• L95-121 js/…/20_theme.ts• L122-226 python/…/capabilities.py• L247-296 python/…/_svg.py• L1363-1377
Change checklist
When these files or sections are changed, remember to consider these:
- Keep theme token names aligned with the browser readers and the static writers. A token that reaches only one surface creates a silent visual split.
- Keep the mark and axis style vocabularies aligned with the renderer capability record. A newly accepted property must have a clear result on every promised renderer.
- Keep custom palette and colormap values fixed when they feed indexed lookup tables, density paint, or static output. Browser-only values can collapse categories or disagree with the colorbar.
- Keep legend category expansion aligned between the live legend and static legend writers. One categorical trace must describe the same category rows on every surface.
- Keep annotation geometry and label ownership separate. A mark-owned label must follow trace or category visibility, while an author annotation must remain independent.
- Keep font metric data in step with the font used by native text. Changes can alter axis gutters, legend fit, and annotation clearance without changing chart data.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A browser-only color is used as a palette or colormap stop | Indexed colors cannot stay stable across density, static, and browser surfaces | Resolve palette and ramp stops to fixed colors before chart build |
| A CSS property is used on a mark family that cannot render it | The chart rejects the style instead of silently producing different output by renderer | Use the mark-family style vocabulary |
| A host CSS class is expected to style native SVG or PNG output | The file has no host cascade, so the visual rule is absent | Put export-critical values on the chart or use a browser-backed export |
| A categorical legend row is hidden without applying the same visibility to geometry | The legend and chart disagree about what is shown | Use the live legend visibility path, which carries trace or category state |
| A rule or band annotation is added to a polar chart | The chart refuses a Cartesian approximation | Use point-anchored polar annotations |
| Native text is given a custom font family | The native bitmap path keeps its embedded face | Use browser-backed or vector output for custom-font fidelity |
Key files
| File | Why to read it |
|---|---|
| python/…/components.py | Defines the public theme, legend, and colorbar concepts and their chart-wide options. |
| python/…/styles.py | Defines the shared mark and axis styling vocabulary. |
| python/…/_annotations.py | Defines annotation types, coordinate rules, and the common annotation specification. |
| python/…/_legendfit.py | Explains content-aware legend placement and its view-aware scoring model. |
| python/…/_fontmetrics.py | Shows the text-width model used by static layout. |
| python/…/_svg.py | Shows which theme, slot, legend, colorbar, and text values reach vector output. |
| python/…/_raster.py | Shows the native static treatment of annotations, legends, colorbars, and text. |
| python/…/dom.py | Lists the stable browser slots available to classes and slot styles. |
| js/…/20_theme.ts | Shows browser token resolution, color-mode behavior, and chrome defaults. |
| js/…/10_colormaps.ts | Shows the shared built-in and custom colormap model. |
| js/…/51_annotations.ts | Shows browser annotation shapes, labels, collision handling, and legend-linked suppression. |
| src/css.rs | Defines safe CSS value and color validation shared by the chart styling boundary. |
| src/font.rs | Contains the embedded native font data used for raster text. |