MultiPanel

class stonerplots.MultiPanel(panels, figure=None, sharex=False, sharey=False, adjust_figsize=True, label_panels=True, same_aspect=True, transpose=False, **kwargs)

Bases: _PlotContextSequence, _PreserveFigureMixin

Context manager for creating multi-panel plots in Matplotlib.

The MultiPanel class simplifies the process of creating and managing multi-panel plots. It supports both regular and irregular grids of subplots, automatically adjusts figure sizes, supports shared axes options, and can apply labeling patterns to subplots.

Args:
panels (tuple[int, int], int, or list[int]):

Specifies the number of subplots to create. Options:

  • tuple(rows, columns): Regular grid with the specified number of rows and columns.

  • int: Single row grid with n columns.

  • list[int]: Irregular grid where each element specifies the number of panels in a row.

Keyword Args:
figure (matplotlib.figure.Figure):

Figure to contain the subplots. Defaults to the current active figure if None.

sharex (bool):

Enables shared x-axis among subplots. Default is False.

sharey (bool):

Enables shared y-axis among subplots. Default is False.

adjust_figsize (bool, float, or tuple[float, float]):

Adjusts figure size to fit the subplots. Options:

  • True (default): Automatically adjusts width/height using pre-defined factors for extra rows/columns.

  • float: Applies a uniform factor for width and height adjustment.

  • tuple[float, float]: Separately adjusts width and height with the provided factors.

label_panels (str or bool):

Adds subplot labels (e.g., “(a)”, “(b)”). Default of True applies the ({alpha}) format. If a string is provided, it is used as the format pattern.

same_aspect (bool):

Ensures all subplots have the same aspect ratio. Ignored if width or height ratios are specified in GridSpec-related configurations. Default is True.

transpose (bool):

Interprets the grid layout based on transposed rows and columns. For irregular grids, rows are treated as columns if transpose=True. Default is False.

**kwargs:

Additional keyword arguments passed down to:

Returns:
(List[matplotlib.axes.Axes]):

List of the created Matplotlib Axes.

Notes:
  • For wide multi-column plots in journal submissions, use double-width figure styles along with adjust_figsize to maintain figure height while increasing width.

  • The nplots parameter is deprecated. Use the panels argument instead to specify the number of subplots.

Examples:

Create a 2x3 grid with shared x-axis and labeled panels:

>>> with MultiPanel((2, 3), sharex=True, label_panels=True) as axes:
...     for ax in axes:
...         ax.plot([1, 2, 3], [4, 5, 6])

Create an irregular grid with 2 rows (3 panels in the first row, 1 in the second):

>>> with MultiPanel([3, 1], adjust_figsize=(0.5, 1.0)) as axes:
...     axes[0].plot([1, 2, 3], [3, 2, 1])
...     axes[1].scatter([1, 2, 3], [4, 5, 6])