alphapepttools.pl.label_axes

Contents

alphapepttools.pl.label_axes#

alphapepttools.pl.label_axes(ax, xlabel=None, ylabel=None, title=None, label_parser=None, enumeration=None, enumeration_xytext=(-10, 10))#

Apply formatted labels and optional enumeration to a matplotlib axes object

Sets axis labels, title, and optional subplot enumeration (e.g., A, B, C) with consistent formatting from alphapepttools configuration. Labels can be optionally processed through a parser function to transform computational names into presentation-ready text. Enumeration letters are positioned in the top-left corner for multi-panel figures.

Parameters:
  • ax (Axes) – The axes object to apply labels to

  • xlabel (str | None (default: None)) – The x-axis label, by default None (existing label is not changed)

  • ylabel (str | None (default: None)) – The y-axis label, by default None (existing label is not changed)

  • title (str | None (default: None)) – The title of the axes, by default None (existing title is not changed)

  • label_parser (Callable | None (default: None)) – A function to parse labels, by default None. This is useful to convert labels from a computation-context to presentation context, e.g. a column like upregulated_proteins could be shown as “Upregulated Proteins” in the plot

  • enumeration (str | None (default: None)) – A string to enumerate the plot in the top left, e.g. “A”, “B”, “C”, etc.

  • enumeration_xytext (tuple[float, float] (default: (-10, 10))) – The offset of the enumeration text in typographic points relative to the top left of the axis. This does not scale with resolution or plot size, but can be adapted to fit the plot. Default is (-10, 10)

Return type:

None

Returns:

None

Examples

Label a single plot with title and axis labels:

import matplotlib.pyplot as plt
import numpy as np
from alphapepttools.pl.figure import label_axes

fig, ax = plt.subplots(figsize=(6, 4))

x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))

label_axes(ax, xlabel="Time (seconds)", ylabel="Amplitude", title="Sine Wave")
plt.show()

Create a multi-panel figure with enumeration:

import matplotlib.pyplot as plt
import numpy as np
import string
from alphapepttools.pl.figure import label_axes

fig, axes = plt.subplots(2, 2, figsize=(10, 8))
axes = axes.flatten()

for i, ax in enumerate(axes):
    x = np.linspace(0, 10, 100)
    ax.plot(x, np.sin(x * (i + 1)))

    label_axes(
        ax,
        xlabel="X values",
        ylabel="Y values",
        title=f"Function {i + 1}",
        enumeration=string.ascii_uppercase[i],
    )

plt.tight_layout()
plt.show()

Use a label parser to format computational names:

import matplotlib.pyplot as plt
from alphapepttools.pl.figure import label_axes

def format_label(label):
    if label is None:
        return None
    # Convert underscore notation to title case
    return label.replace("_", " ").title()


fig, ax = plt.subplots(figsize=(6, 4))
ax.scatter([1, 2, 3], [4, 5, 6])

label_axes(
    ax,
    xlabel="sample_id",
    ylabel="protein_abundance",
    title="abundance_distribution",
    label_parser=format_label,
)
# Results in: "Sample Id", "Protein Abundance", "Abundance Distribution"
plt.show()

Notes

The function applies labels with font sizes from the global configuration. If a label_parser is provided, it will be applied to all labels (xlabel, ylabel, and title) before setting them. The enumeration text is positioned using matplotlib’s annotation system with offset points for precise placement.