alphapepttools.pl.create_figure

Contents

alphapepttools.pl.create_figure#

alphapepttools.pl.create_figure(nrows=1, ncols=1, figsize=None, height_ratios=None, width_ratios=None, subplots_kwargs=None, gridspec_kwargs=None)#

Create a figure with a specified number of rows and columns

Returns an AxisManager object to manage axes objects. This is especially useful for creating subplots with consistent styling. Importantly, it should completely sync the plot a user sees in a jupyter notebook with the exported (e.g. .png) figure file. The aim of this is to entirely eliminate time consuming iterations of checking the exported plot and going back to adjust sizes/padding in the code.

Parameters:
  • nrows (int (default: 1)) – The number of rows in the figure, by default 1

  • ncols (int (default: 1)) – The number of columns in the figure, by default 1

  • figsize (tuple[float, float] | tuple[str, str] | None (default: None)) – The size of the figure in inches. If a tuple of strings is provided, the strings must be valid keys in the config file, by default None

  • height_ratios (list[float] | None (default: None)) – The height ratios of the rows in the figure, by default None

  • width_ratios (list[float] | None (default: None)) – The width ratios of the columns in the figure, by default None

  • subplots_kwargs (dict | None (default: None)) – Additional keyword arguments to pass to plt.subplots, by default None

  • gridspec_kwargs (dict | None (default: None)) – Additional keyword arguments for gridspec configuration, by default None

Return type:

tuple[Figure, ndarray]

Returns:

fig

The figure object

axm

An iterable and indexable AxisManager object to manage matplotlib.axes objects

Examples

Create a 2x2 grid of subplots:

import numpy as np
from alphapepttools.pl.figure import create_figure

fig, axm = create_figure(nrows=2, ncols=2, figsize=(4, 4))
x = np.linspace(0, 10, 100)
functions = [lambda x: np.sin(x + 1), lambda x: np.sin(x) * 2, lambda x: np.sin(x) + 2, lambda x: np.sin(x) - 2]
for i, func in enumerate(functions):
    ax = axm[i]
    ax.scatter(x, func(x))

Create the same plots in a 1x4 layout:

import numpy as np
from alphapepttools.pl.figure import create_figure

fig, axm = create_figure(nrows=1, ncols=4, figsize=(8, 2))
x = np.linspace(0, 10, 100)
functions = [lambda x: np.sin(x + 1), lambda x: np.sin(x) * 2, lambda x: np.sin(x) + 2, lambda x: np.sin(x) - 2]
for i, func in enumerate(functions):
    ax = axm[i]
    ax.scatter(x, func(x))