pub struct Figure { /* private fields */ }Expand description
A figure is the top-level container for one or more axes (subplots).
The figure owns all axes, holds the overall dimensions, an optional
super-title, and the visual theme. It implements the rendering pipeline
described in ARCHITECTURE.md:
- Fill figure background.
- Draw suptitle if set.
- Compute subplot layout rectangles.
- For each axes: delegate rendering with its allocated rectangle and theme.
§Ownership model (ADR-003)
Figure owns Vec<Axes> directly – no Rc, no RefCell.
add_subplot returns &mut Axes, and the borrow
checker enforces single-axes mutation at compile time.
§Examples
use plotkit_core::figure::Figure;
let mut fig = Figure::new();
let ax = fig.add_subplot(1, 1, 1);
// ax.plot(...)?;Implementations§
Source§impl Figure
impl Figure
Sourcepub fn with_size(width: u32, height: u32) -> Self
pub fn with_size(width: u32, height: u32) -> Self
Creates a new figure with the specified dimensions in pixels.
Sourcepub fn add_subplot(
&mut self,
nrows: usize,
ncols: usize,
index: usize,
) -> &mut Axes
pub fn add_subplot( &mut self, nrows: usize, ncols: usize, index: usize, ) -> &mut Axes
Adds a subplot and returns a mutable reference to it.
Uses 1-based indexing in matplotlib style: (nrows, ncols, index).
The index counts across rows first (row-major), starting at 1.
If a subplot already exists at the given index, the existing axes
is returned without creating a duplicate. Otherwise a new Axes
is created, appended to the figure’s axes list, and returned.
§Panics
Panics if nrows, ncols, or index is zero, or if index exceeds
nrows * ncols.
§Examples
use plotkit_core::figure::Figure;
let mut fig = Figure::new();
let ax = fig.add_subplot(2, 2, 1); // top-left of a 2x2 gridSourcepub fn suptitle(&mut self, title: &str) -> &mut Self
pub fn suptitle(&mut self, title: &str) -> &mut Self
Sets the overall figure title (super-title), displayed above all subplots.
Returns &mut Self for builder-style chaining.
Sourcepub fn set_theme(&mut self, theme: Theme) -> &mut Self
pub fn set_theme(&mut self, theme: Theme) -> &mut Self
Sets the visual theme for the entire figure.
The theme is inherited by all axes during rendering unless an individual axes has its own override.
Returns &mut Self for builder-style chaining.
Sourcepub fn axes_mut(&mut self, index: usize) -> Option<&mut Axes>
pub fn axes_mut(&mut self, index: usize) -> Option<&mut Axes>
Returns mutable access to the axes at index (0-based).
Returns None if index is out of bounds.
Sourcepub fn axes(&self, index: usize) -> Option<&Axes>
pub fn axes(&self, index: usize) -> Option<&Axes>
Returns a shared reference to the axes at index (0-based).
Returns None if index is out of bounds.
Sourcepub fn subplots(nrows: usize, ncols: usize) -> Self
pub fn subplots(nrows: usize, ncols: usize) -> Self
Creates a new figure with an nrows × ncols subplot grid.
Axes are added in row-major order (index 0 = top-left). Access them
via fig.axes_mut(index) where index goes from 0 to nrows*ncols - 1.
§Panics
Panics if nrows or ncols is zero.
Sourcepub fn subplots_with_size(
nrows: usize,
ncols: usize,
width: u32,
height: u32,
) -> Self
pub fn subplots_with_size( nrows: usize, ncols: usize, width: u32, height: u32, ) -> Self
Creates a new figure with an nrows × ncols subplot grid and custom size.
§Panics
Panics if nrows or ncols is zero.
Sourcepub fn axes_grid(
&mut self,
row: usize,
col: usize,
ncols: usize,
) -> Option<&mut Axes>
pub fn axes_grid( &mut self, row: usize, col: usize, ncols: usize, ) -> Option<&mut Axes>
2D indexing into the subplot grid.
Returns axes_mut(row * ncols + col), or None if out of bounds.
Sourcepub fn twinx(&mut self, parent_index: usize) -> &mut Axes
pub fn twinx(&mut self, parent_index: usize) -> &mut Axes
Creates a twin axes that shares the x-axis of the axes at parent_index
but has an independent y-axis drawn on the right side.
§Panics
Panics if parent_index is out of bounds or if the parent already
has a twin.
Sourcepub fn twiny(&mut self, parent_index: usize) -> &mut Axes
pub fn twiny(&mut self, parent_index: usize) -> &mut Axes
Creates a twin axes that shares the y-axis of the axes at parent_index
but has an independent x-axis drawn on the top side.
§Panics
Panics if parent_index is out of bounds or if the parent already
has a twin.
Sourcepub fn twin_of(&self, parent_index: usize) -> Option<usize>
pub fn twin_of(&self, parent_index: usize) -> Option<usize>
Returns the twin axes index for a given parent axes index, if one exists.
Sourcepub fn render(&self, renderer: &mut impl Renderer)
pub fn render(&self, renderer: &mut impl Renderer)
Renders the figure using the given renderer.
This is the core rendering pipeline (per ARCHITECTURE.md):
- Fill figure background with the theme’s
figure_backgroundcolor. - Draw suptitle if one has been set.
- Compute the subplot grid layout, producing one
Rectper axes. - For each axes, delegate to
Axes::renderwith its assigned rectangle and the figure theme.