carabiner.mpl package
Submodules
carabiner.mpl.utils module
Utilities for matplotlib.
- carabiner.mpl.utils.add_legend(ax: Axes, **kwargs) Legend[source]
Add a legend to the right of a Matplotlib plotting axis.
Uses a sensible default for putting the legend out of the way. Keyword arguments override loc and bbox_to_anchor, and additional arguments are passed to matplotlib.axes.Axes.legend().
- Parameters:
ax (matplotlib.axes.Axes) – Axes to add a legend to.
- Return type:
matplotlib.legend.Legend
- carabiner.mpl.utils.figsaver(output_dir: str = '.', prefix: str | None = None, dpi: int = 300, format: str | Iterable[str] = 'png') Callable[[Figure, str, int, str, DataFrame | None], None][source]
Create a function to save figures in a predefined location.
- Parameters:
output_dir (str, optional) – Directory to save figures. Default: “.”.
prefix (str, optional) – Prefix for filenames. Default: no prefix.
dpi (int, optional) – Resolution of saved figures. Default: 300.
format (str or Iterable, optional) – File format(s) of figures. Default: “png”.
- Returns:
A function taking Figure, name, and optionally a Pandas DataFrame as arguments. Saves as {dir}/{prefix}{name}.{format}. If a DataFrame is provided, it as saved as {dir}/{prefix}{name}.csv.
- Return type:
Callable
- carabiner.mpl.utils.grid(nrow: int = 1, ncol: int = 1, panel_size: float = 3.0, aspect_ratio: float = 1.0, layout: str = 'constrained', sharex: str | bool = False, sharey: str | bool = False, hide_shared_ticks: bool = False, square: bool = False, *args, **kwargs) Tuple[Figure, Axes][source]
Create a figure and a set of subplots with sensible defaults.
Additional arguments are passed to matplotlib.pyplot.subplots().
- Parameters:
nrow (int, optional) – Number of rows. Default: 1.
ncol (int, optional) – Number of columns. Default: 1.
panel_size (float, optional) – Size of panels. Default: 3.
aspect_ratio (float) – Ratio of width over height. Default: 1 (square).
layout (str) – Matplotlib fig layout. Default: “constrained”.
hide_shared_ticks (bool, optional) – Whether to hide the ticks when axes have shared scales from setting sharex or sharey. Default: False.
square (bool) – Whether to force panels to be square. Default: True.
- Returns:
Pair of figure.Figure and axes.Axes objects.
- Return type:
tuple
- carabiner.mpl.utils.scattergrid(df: DataFrame, grid_columns: str | Iterable[str], grid_rows: str | Iterable[str] | None = None, grouping: str | Iterable[str] | None = None, log: str | Iterable[str] | None = None, n_bins: int = 40, scatter_opts: Mapping[str, Any] | None = None, hist_opts: Mapping[str, Any] | None = None, legend_opts: Mapping[str, Any] | None = None, *args, **kwargs) Tuple[Figure, Axes][source]
Create a scatter plot to compare sets of variables in a Pandas DataFrame.
Similar to pandas.plotting.scatter_matrix, but with larger panels and control over which variables are log-scaled.
Additional arguments are passed to grid().
- Parameters:
df (pandas.DataFrame) – Data to plot.
grid_columns (str | Iterable[str]) – Data columns to plot along the rows of the scatter grid. Becomes the x-axes.
grid_rows (str | Iterable[str], optional) – Data columns to plot down the columns of the scatter grid. Becomes the y-axes. If not provided, uses grid_columns for all pair-wise comparison.
grouping (str | Iterable[str], optional) – If provided, use these columns of df to make groups and plot each data group as a differnt color.
log (str | Iterable[str], optional) – If provided, plot these columns of df on a log scale.
n_bins (int, optional) – Number of bin for histograms plotted on identity diagonal of the scatter grid. Default: 40.
scatter_opts (dict, optional) – Extra keyword arguments to pass to the Matplotlib scatter plots.
hist_opts (dict, optional) – Extra keyword arguments to pass to the Matplotlib histogram plots.
legend_opts (dict, optional) – Extra keyword arguments to pass to the Matplotlib legend.
- Returns:
Pair of figure.Figure and axes.Axes objects.
- Return type:
tuple
- Raises:
KeyError – If no named columns are in df.