Backends API¶
BackendProtocol¶
BackendProtocol
¶
Bases: Protocol[FigureT]
Structural protocol that every cplots backend must satisfy.
A backend translates a :class:~cplots.core.spec.PlotSpec or
:class:~cplots.core.spec.FigureSpec into a native figure object.
Any class that implements these methods is a valid backend — no
inheritance required.
Styling: the spec's style (a :class:~cplots.core.theme.Style, possibly
None or with unset fields) applies to the figure being rendered only. A
backend must not change process-wide state such as matplotlib's rcParams
beyond the render (use matplotlib.rc_context).
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Unique backend identifier (e.g. |
Methods:¶
render
¶
Render a single-panel plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
PlotSpec
|
Backend-agnostic plot specification. |
required |
Returns:
| Type | Description |
|---|---|
FigureT
|
Native figure object. |
render_figure
¶
Render a multi-panel figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
FigureSpec
|
Backend-agnostic multi-panel figure specification. |
required |
Returns:
| Type | Description |
|---|---|
FigureT
|
Native figure object. |
show
¶
Display the native figure interactively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
FigureT
|
Native figure returned by :meth: |
required |
**kwargs
|
Any
|
Backend-specific display options. |
{}
|
save
¶
Save the native figure to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
FigureT
|
Native figure returned by :meth: |
required |
path
|
str | PathLike[str]
|
Destination file path. |
required |
**kwargs
|
Any
|
Backend-specific save options (e.g. |
{}
|
BaseBackend¶
BaseBackend
¶
Optional base class for backends that want automatic registration.
Subclass with the name keyword to self-register on class creation::
class MyBackend(BaseBackend, name="mybackend"):
def render(self, spec): ...
...
The name keyword triggers :func:~cplots.core.registry.register_backend
at class-definition time so no manual registration call is required.
Trace dispatch: :meth:_dispatch calls self._draw_<snake_case_type>(trace, ctx)
(e.g. _draw_band_trace for a :class:~cplots.core.spec.BandTrace) and raises
a uniform NotImplementedError for trace types the backend has no method for.
Backends that don't draw trace by trace can check up front with :meth:_require.
Presentation features: :attr:capabilities lists the features of
:data:cplots.core.features.FEATURES the backend renders; :meth:_check_features
warns once about any other feature a spec asks for.
MatplotlibBackend¶
MatplotlibBackend
¶
Bases: BaseBackend
Matplotlib backend for cplots.
Produces matplotlib.figure.Figure objects. Registered automatically
under the name "matplotlib".
Requires: pip install cplots[matplotlib]
Methods:¶
render
¶
Render a single PlotSpec to a matplotlib Figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
PlotSpec
|
Backend-agnostic plot specification. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A |
render_figure
¶
Render a multi-panel FigureSpec to a matplotlib Figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
FigureSpec
|
Backend-agnostic multi-panel figure specification. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
For 3D panels or TrianglePlot panels. |
save
¶
Write fig to path and close it (save() renders a fresh figure each
time, so keeping it open would only accumulate figures in batch scripts).
PlotlyBackend¶
PlotlyBackend
¶
Bases: BaseBackend
Plotly backend for cplots.
Produces plotly.graph_objects.Figure objects. Registered automatically
under the name "plotly".
When latex_labels=True on the spec, axis labels and titles are passed
through unchanged so MathJax can render them in HTML export.
Requires: pip install cplots[plotly]
Methods:¶
render
¶
Render a single PlotSpec to a Plotly Figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
PlotSpec
|
Backend-agnostic plot specification. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A |
render_figure
¶
Render a multi-panel FigureSpec to a Plotly Figure with subplots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
FigureSpec
|
Backend-agnostic multi-panel figure specification. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A |
save
¶
Write fig: .html/.htm as interactive HTML, anything else as an image.
Images need kaleido; HTML does not.
AltairBackend¶
AltairBackend
¶
Bases: BaseBackend
Altair (Vega-Lite) backend for cplots.
Produces altair.Chart objects. Registered automatically under the name
"altair".
Note
Rowspan > 1 in multi-panel layouts is not supported; use the matplotlib or plotly backend for spanning layouts. Traces with typed styling (alpha, line width/style, markers, error bars) are drawn as separate layers without a legend entry; plain traces share one legend.
Requires: pip install cplots[altair]
Methods:¶
render
¶
Render a single PlotSpec to an Altair Chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
PlotSpec
|
Backend-agnostic plot specification. |
required |
interactive
|
bool
|
Whether to call |
True
|
Returns:
| Type | Description |
|---|---|
Chart
|
An |
render_figure
¶
Render a multi-panel FigureSpec as an Altair hconcat/vconcat chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
FigureSpec
|
Backend-agnostic multi-panel figure specification. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
An |
Any
|
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If any panel has |