Backends API¶
BackendProtocol¶
BackendProtocol
¶
Bases: Protocol[FigureT_co]
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.
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_co
|
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_co
|
Native figure object. |
show
¶
Display the native figure interactively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
FigureT_co
|
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_co
|
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.
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 closed |
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 closed |
apply_theme
¶
Apply a ThemeSpec to matplotlib's global rcParams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
ThemeSpec
|
Theme to apply. |
required |
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 |
apply_theme
¶
No-op: Plotly themes are applied per-render via layout kwargs.
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.
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 |