solarwindpy.plotting.tools
Utility functions for common matplotlib tasks.
These helpers provide shortcuts for creating figures, saving output, and building grids of axes with shared colorbars.
Functions
|
Build an array of axes that share a colour bar. |
Determine a sensible |
|
|
Create a combined legend for multiple axes. |
|
Create a grid of axes that share a single colorbar. |
|
Save a figure in both PDF and PNG formats. |
|
Create a grid of subplots with a scaled figure size. |
- subplots(nrows=1, ncols=1, scale_width=1.0, scale_height=1.0, **kwargs)[source]
Create a grid of subplots with a scaled figure size.
- Parameters:
nrows (int, optional) – Number of subplot rows.
ncols (int, optional) – Number of subplot columns.
scale_width (float, optional) – Factor applied to the default figure width.
scale_height (float, optional) – Factor applied to the default figure height.
**kwargs – Additional keyword arguments passed directly to
matplotlib.pyplot.subplots().
- Returns:
fig (
matplotlib.figure.Figure)ax (
matplotlib.axes.Axesor array of Axes)
Examples
>>> fig, ax = subplots(2, 2, scale_width=1.5)
- save(fig, spath, add_info=True, info_x=0, info_y=0, log=True, pdf=True, png=True, **kwargs)[source]
Save a figure in both PDF and PNG formats.
- Parameters:
fig (
matplotlib.figure.Figureormatplotlib.axes.Axes) – The figure or axis to save.spath (
pathlib.Path) – Base path for the output files. The appropriate extension will be added automatically.add_info (bool, optional) – If
True, add an attribution and timestamp to the bottom left of the PNG version.info_x (float, optional) – X-position of the attribution text in figure coordinates.
info_y (float, optional) – Y-position of the attribution text in figure coordinates.
log (bool, optional) – If
True, write information about the saved files toalog.pdf (bool, optional) – Save a PDF version of the figure.
png (bool, optional) – Save a PNG version of the figure.
**kwargs – Additional keyword arguments passed to
Figure.savefig().
- Return type:
None
Examples
>>> fig, ax = subplots() >>> save(fig, Path('my_plot'))
- joint_legend(*axes, idx_for_legend=-1, **kwargs)[source]
Create a combined legend for multiple axes.
- Parameters:
*axes (
matplotlib.axes.Axes) – Axes objects from which to collect legend handles and labels.idx_for_legend (int, optional) – Index of the axis (after flattening) on which to place the legend. By default the legend is placed on the last axis.
idx_for_legend=-1assumes that the last axis is on the right hand side of the figure.**kwargs – Extra keyword arguments forwarded to
Axes.legend().
- Returns:
legend
- Return type:
Examples
>>> fig, ax = subplots(1, 2) >>> ax[0].plot([1, 2], label='a') [<matplotlib.lines.Line2D object at 0x...>] >>> ax[1].plot([2, 3], label='b') [<matplotlib.lines.Line2D object at 0x...>] >>> joint_legend(ax[0], ax[1]) <matplotlib.legend.Legend object at 0x...>
- multipanel_figure_shared_cbar(nrows: int, ncols: int, vertical_cbar: bool = True, sharex: bool = True, sharey: bool = True, **kwargs)[source]
Create a grid of axes that share a single colorbar.
This is a lightweight wrapper around
build_ax_array_with_common_colorbar()for backward compatibility.- Parameters:
nrows (int) – Shape of the axes grid.
ncols (int) – Shape of the axes grid.
vertical_cbar (bool, optional) – If
Truethe colorbar is placed to the right of the axes; otherwise it is placed above them.sharex (bool, optional) – If
Trueshare the respective axis limits across all panels.sharey (bool, optional) – If
Trueshare the respective axis limits across all panels.**kwargs – Additional arguments controlling layout such as
figsizeor grid ratios.
- Returns:
fig (
matplotlib.figure.Figure)axes (ndarray of
matplotlib.axes.Axes)cax (
matplotlib.axes.Axes)
Examples
>>> fig, axs, cax = multipanel_figure_shared_cbar(2, 2)
- build_ax_array_with_common_colorbar(nrows=1, ncols=1, cbar_loc='top', fig_kwargs=None, gs_kwargs=None)[source]
Build an array of axes that share a colour bar.
- Parameters:
nrows (int, optional) – Desired grid shape.
ncols (int, optional) – Desired grid shape.
cbar_loc ({"top", "bottom", "left", "right"}, optional) – Location of the colorbar relative to the axes grid.
fig_kwargs (dict, optional) – Keyword arguments forwarded to
matplotlib.pyplot.figure().gs_kwargs (dict, optional) – Additional options for
matplotlib.gridspec.GridSpec.
- Returns:
fig (
matplotlib.figure.Figure)axes (ndarray of
matplotlib.axes.Axes)cax (
matplotlib.axes.Axes)
Examples
>>> fig, axes, cax = build_ax_array_with_common_colorbar(2, 3, cbar_loc='right')
- calculate_nrows_ncols(n)[source]
Determine a sensible
(nrows, ncols)pair fornaxes.The heuristic attempts to generate a nearly square layout while also taking typical display aspect ratios into account.
- Parameters:
n (int) – Total number of axes required.
- Returns:
nrows (int)
ncols (int)
Examples
>>> calculate_nrows_ncols(5) (...2..., ...3...)