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) -> Figure
pub fn with_size(width: u32, height: u32) -> Figure
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 Figure
pub fn suptitle(&mut self, title: &str) -> &mut Figure
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 Figure
pub fn set_theme(&mut self, theme: Theme) -> &mut Figure
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) -> Figure
pub fn subplots(nrows: usize, ncols: usize) -> Figure
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,
) -> Figure
pub fn subplots_with_size( nrows: usize, ncols: usize, width: u32, height: u32, ) -> Figure
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.
Sourcepub fn render_to<R>(&self, renderer: R) -> Vec<u8> ⓘwhere
R: Renderer,
pub fn render_to<R>(&self, renderer: R) -> Vec<u8> ⓘwhere
R: Renderer,
Renders the figure using the given renderer and returns the encoded output bytes (PNG, SVG, PDF, etc., depending on the renderer).
This convenience method calls render and then
Renderer::finalize to produce the final byte output.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Figure
impl RefUnwindSafe for Figure
impl Send for Figure
impl Sync for Figure
impl Unpin for Figure
impl UnsafeUnpin for Figure
impl UnwindSafe for Figure
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more