larvaworld.lib.plot.base

Basic plotting classes

Classes

BasePlot

Base class for creating customizable matplotlib plots.

AutoBasePlot

Automatic plot generation with immediate figure building.

AutoPlot

Automatic plot generation with larvaworld dataset integration.

GridPlot

Multi-panel grid layout for composite plots.

Module Contents

class larvaworld.lib.plot.base.BasePlot(name: str = 'larvaworld_plot', save_as: str | None = None, pref: str | None = None, suf: str = 'pdf', save_to: str | None = None, subfolder: str | None = None, return_fig: bool = False, show: bool = False, subplot_kw: Dict[str, Any] = {}, build_kws: Dict[str, Any] = {}, **kwargs: Any)

Base class for creating customizable matplotlib plots.

Provides common functionality for plot generation, styling, and output management. Handles figure/axes creation, saving, and display options. Subclasses implement specific plot types by overriding plot methods.

Attributes:

filename: Output filename with extension fit_filename: Filename for fit data CSV save_to: Directory path for saving plots return_fig: Whether to return figure object show: Whether to display plot immediately build_kws: Keyword arguments for figure building

Example:
>>> plotter = BasePlot(name='myplot', save_to='./plots', suf='png')
>>> plotter.build(nrows=2, ncols=2)
>>> # ... add plot content ...
>>> plotter.save()
filename = 'None.pdf'
fit_filename = 'None_fits.csv'
fit_ind = None
fit_df = None
save_to = None
return_fig = False
show = False
build_kws
build(fig: matplotlib.figure.Figure | None = None, axs: Axes | Sequence[Axes] | None = None, dim3: bool = False, azim: int = 115, elev: int = 15) None

Method that defines the figure and axes on which to draw. These can be provided externally as arguments to create a composite figure. Otherwise they are created independently.

Args:

fig: The figure of the plot (optional) axs: The axes of the figure (optional) dim3: Whether the figure will be 3-dimensional. Default : False azim: The azimuth of a 3D figure. Default : 115 elev: The elevation of a 3D figure. Default : 15

property Naxs: int
property Ncols: int
property Nrows: int
conf_ax(idx: int = 0, ax: matplotlib.axes.Axes | None = None, xlab: str | None = None, ylab: str | None = None, zlab: str | None = None, xlim: Sequence[float] | None = None, ylim: Sequence[float] | None = None, zlim: Sequence[float] | None = None, xticks: Sequence[float] | None = None, xticklabels: Sequence[str] | None = None, yticks: Sequence[float] | None = None, xticklabelrotation: int | None = None, yticklabelrotation: int | None = None, yticklabels: Sequence[str] | None = None, zticks: Sequence[float] | None = None, zticklabels: Sequence[str] | None = None, xtickpos: str | None = None, xtickpad: int | None = None, ytickpad: int | None = None, ztickpad: int | None = None, xlabelfontsize: int | None = None, ylabelfontsize: int | None = None, xticklabelsize: int | None = None, yticklabelsize: int | None = None, zticklabelsize: int | None = None, major_ticklabelsize: int | None = None, minor_ticklabelsize: int | None = None, xlabelpad: int | None = None, ylabelpad: int | None = None, zlabelpad: int | None = None, equal_aspect: bool | None = None, xMaxN: int | None = None, yMaxN: int | None = None, zMaxN: int | None = None, yStrN: int | None = None, xMath: bool | None = None, yMath: bool | None = None, tickMath: Tuple[int, int] | None = None, ytickMath: Tuple[int, int] | None = None, xMaxFix: bool = False, leg_loc: str | None = None, leg_handles: Sequence[Any] | None = None, leg_labels: Sequence[str] | None = None, legfontsize: int | None = None, xvis: bool | None = None, yvis: bool | None = None, zvis: bool | None = None, title: str | None = None, title_y: float | None = None, titlefontsize: int | None = None) None

Helper method that configures an axis of the figure

conf_ax_3d(vars: Sequence[str], target: str, lims: Tuple[Sequence[float], Sequence[float], Sequence[float]] | None = None, title: str | None = None, maxN: int = 3, labelpad: int = 15, tickpad: int = 5, idx: int = 0) None
adjust(LR: Tuple[float, float] | None = None, BT: Tuple[float, float] | None = None, W: float | None = None, H: float | None = None) None
set(fig: matplotlib.figure.Figure) None
get() Any
conf_fig(adjust_kws: Dict[str, Any] | None = None, align: Sequence[matplotlib.axes.Axes] | Sequence[Any] | None = None, title: str | None = None, title_kws: Dict[str, Any] = {}) None
class larvaworld.lib.plot.base.AutoBasePlot(fig: matplotlib.figure.Figure | None = None, axs: Axes | Sequence[Axes] | None = None, dim3: bool = False, azim: int = 115, elev: int = 15, **kwargs: Any)

Bases: BasePlot

Automatic plot generation with immediate figure building.

Extends BasePlot by automatically calling build() during initialization, creating the matplotlib figure and axes immediately. Supports both 2D and 3D plots with customizable viewing angles.

Attributes:

fig: Matplotlib Figure object ax: Matplotlib Axes object (or array of Axes for subplots) dim3: Whether plot is 3D

Example:
>>> plot = AutoBasePlot(nrows=2, ncols=2, dim3=False)
>>> plot.ax[0, 0].plot(x, y)  # Use axes directly
>>> plot.save()
class larvaworld.lib.plot.base.AutoPlot(ks: Sequence[str] = [], key: str = 'step', klabels: Dict[str, str] = {}, datasets: Sequence[Any] = [], labels: Sequence[str] | None = None, colors: Sequence[Any] | None = None, add_samples: bool = False, ranges: Sequence[Any] | None = None, absolute: bool = False, rad2deg: bool = False, space_unit: str = 'mm', **kwargs: Any)

Bases: AutoBasePlot, larvaworld.lib.process.LarvaDatasetCollection

Automatic plot generation with larvaworld dataset integration.

Combines AutoBasePlot functionality with LarvaDatasetCollection to enable direct plotting from larvaworld LarvaDataset objects. Handles multiple datasets with automatic labeling, coloring, and unit conversion.

Attributes:

datasets: Collection of LarvaDataset objects labels: Dataset labels for legend colors: Colors for each dataset ks: Parameter keys to plot key: Indexing key (‘step’, ‘time’, etc.) klabels: Custom labels for parameters Ndatasets: Number of datasets Nks: Number of parameters

Example:
>>> plot = AutoPlot(datasets=[d1, d2], labels=['Control', 'Test'])
>>> plot.plot(ks=['v', 'a'])  # Plot velocity and acceleration
>>> plot.save()
key = 'step'
ks = []
kkdict
pdict
vdict
dkdict
pars
Nks = 0
ranges = None
absolute = False
rad2deg = False
comp_all_pvalues() None
plot_all_half_circles() None
plot_half_circle(ax: matplotlib.axes.Axes, col1: Any, col2: Any, v: int, pv: float, coef: int = 0) bool
data_leg(idx: int | None = None, labels: Sequence[str] | None = None, colors: Sequence[Any] | None = None, anchor: Tuple[float, float] | None = None, handlelength: float = 0.5, handleheight: float = 0.5, Nagents_in_label: bool = True, **kwargs: Any) Any
plot_quantiles(k: str | None = None, par: str | None = None, idx: int = 0, ax: matplotlib.axes.Axes | None = None, xlim: Sequence[float] | None = None, ylim: Sequence[float] | None = None, ylab: str | None = None, unit: str = 'sec', leg_loc: str = 'upper left', coeff: float = 1, absolute: bool = False, individuals: bool = False, show_first: bool = False, Nagents_in_label: bool = True, **kwargs: Any) None
plot_hist(half_circles: bool = True, use_title: bool = False, par_legend: bool = False, nbins: int = 30, alpha: float = 0.5, ylim: Sequence[float] = [0, 0.2], Nagents_in_label: bool = True, **kwargs: Any) None
boxplots(grouped: bool = False, annotation: bool = True, show_ns: bool = False, target_only: Any = None, stripplot: bool = False, ylims: Sequence[Sequence[float]] | None = None, **kwargs: Any) None
class larvaworld.lib.plot.base.GridPlot(name: str, width: int, height: int, scale: Tuple[int, int] = (1, 1), **kwargs: Any)

Bases: BasePlot

Multi-panel grid layout for composite plots.

Creates a grid-based figure layout using matplotlib GridSpec for organizing multiple subplots. Supports automatic subplot placement with optional lettering (A, B, C, …) and flexible sizing.

Attributes:

width: Number of columns in grid height: Number of rows in grid fig: Matplotlib Figure object grid: GridSpec layout manager cur_w: Current column position cur_h: Current row position letters: List of panel labels letter_dict: Mapping of panel positions to letters

Example:
>>> grid = GridPlot(name='composite', width=3, height=2)
>>> ax1 = grid.add()  # Add first panel
>>> ax2 = grid.add(N=2)  # Add panel spanning 2 columns
>>> grid.save()
fig
grid
cur_idx = 0
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
letter_dict
add(N: int = 1, w: int | None = None, h: int | None = None, w0: int | None = None, h0: int | None = None, dw: int = 0, dh: int = 0, share_w: bool = False, share_h: bool = False, letter: bool = True, x0: bool = False, y0: bool = False, cols_first: bool = False, annotate_all: bool = False) Axes | list[Axes]
add_letter(ax: matplotlib.axes.Axes, letter: bool = True, x0: bool = False, y0: bool = False) None
annotate(dx: float = -0.05, dy: float = 0.005, full_dict: bool = False) None
plot(func: str, kws: Dict[str, Any], axs: Sequence[matplotlib.axes.Axes] | None = None, **kwargs: Any) Any