StackVertical¶
- class stonerplots.StackVertical(number, figure=None, joined=True, sharex=True, sharey=False, adjust_figsize=True, label_panels=True, **kwargs)¶
Bases:
MultiPanel
A context manager for generating a vertical stack of subplots with shared x-axes.
This class creates a vertical stack of subplots, optionally removing the vertical space between them for a clean and compact layout. Automatically handles adjustments to the figure height, subplot alignment, and axes label formatting.
- Args:
- number (int):
The number of subplots to stack vertically.
- Keyword Args:
- figure (matplotlib.Figure):
The figure to use for the subplots. Defaults to the current active figure if None.
- joined (bool):
Whether to remove vertical space between subplots for a seamless look. Default is True.
- sharex (bool):
Whether the subplots share the same x-axis. Default is True.
- sharey (bool):
Whether the subplots share the same y-axis. Default is False.
- adjust_figsize (bool or float):
Whether to adjust the figure height to accommodate additional subplots. Options:
- True (default): Increases figure height by 0.6 of the original height for each
additional subplot beyond the first.
float: Specifies a custom height adjustment factor for each additional subplot.
- label_panels (str or bool):
Adds labels (e.g., “(a)”, “(b)”) to the subplots for clear identification.
Default is True, which applies the ({alpha}) pattern.
A custom string can be provided to format the labels.
- **kwargs:
Additional arguments:
matplitlib.axes.Axes.set_title()
for subplot font adjustments.matplotlib.figure.Figure.add_gridspec()
for grid configuration.
- Returns:
- List[matplotlib.axes.Axes]:
The list of axes created within the context.
- Notes:
Matplotlib’s layout engine can make it challenging to remove vertical space between stacked subplots due to constraints on label and title positioning. This class manages this for you by adjusting y-limits and ensuring tick labels do not overlap.
The current figure during the context is the one with the stacked subplots. Upon exiting the context, the previous active figure is restored.
- Examples:
Create a vertical stack of 3 subplots with shared x-axes:
>>> with StackVertical(3, sharex=True, joined=True) as axes: ... for ax in axes: ... ax.plot([1, 2, 3], [4, 5, 6])