Skip to content

Plotting Module

plotting

plot_utils

latex_plt(matplotlib)

This function updates the matplotlib library to use Latex and changes some default plot parameters.

Parameters:

Name Type Description Default
matplotlib module

The matplotlib module (e.g., import matplotlib) to configure.

required

Returns:

Type Description
module

The updated matplotlib module with LaTeX and custom settings applied.

Source code in allinpy/plotting/plot_utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def latex_plt(matplotlib: types.ModuleType) -> types.ModuleType:
    """This function updates the matplotlib library to use Latex and changes some default plot parameters.

    Parameters
    ----------
    matplotlib : module
        The matplotlib module (e.g., `import matplotlib`) to configure.

    Returns
    -------
    module
        The updated matplotlib module with LaTeX and custom settings applied.
    """

    pgf_with_latex = {
        "axes.labelsize": 6,
        "font.size": 6,
        "legend.fontsize": 6,
        "axes.titlesize": 6,
        "xtick.labelsize": 6,
        "ytick.labelsize": 6,
        "figure.titlesize": 6,
        "pgf.rcfonts": False,
    }
    matplotlib.rcParams.update(pgf_with_latex)

    return matplotlib

cm2inch(*tupl)

This function converts cm to inches.

Obtained from: https://stackoverflow.com/questions/14708695/ specify-figure-size-in-centimeter-in-matplotlib/22787457

Parameters:

Name Type Description Default
tupl float or tuple of float

Size of the plot in centimeters. Can be provided as individual float arguments (e.g., width, height) or as a single tuple of floats.

()

Returns:

Type Description
tuple of float

Converted image size in inches.

Source code in allinpy/plotting/plot_utils.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def cm2inch(*tupl: Union[float, Tuple[float, ...]]) -> Tuple[float, ...]:
    """This function converts cm to inches.

    Obtained from: https://stackoverflow.com/questions/14708695/
    specify-figure-size-in-centimeter-in-matplotlib/22787457

    Parameters
    ----------
    tupl : float or tuple of float
        Size of the plot in centimeters. Can be provided as individual float arguments (e.g., width, height)
        or as a single tuple of floats.

    Returns
    -------
    tuple of float
        Converted image size in inches.

    """

    inch = 2.54
    if isinstance(tupl[0], tuple):
        return tuple(i / inch for i in tupl[0])
    else:
        return tuple(i / inch for i in tupl)

label_subplots(f, texts, x_offset=-0.07, y_offset=0.015)

This function labels the subplots.

Obtained from: https://stackoverflow.com/questions/52286497/ matplotlib-label-subplots-of-different-sizes-the-exact-same-distance-from-corner

Parameters:

Name Type Description Default
f Figure

Matplotlib figure handle containing the subplots.

required
texts sequence of str

List of labels for each subplot (e.g., ["A", "B", "C"]).

required
x_offset float or sequence of float

Horizontal offset(s) for the subplot labels. If a single float, the same offset is applied to all subplots. Default is -0.07.

-0.07
y_offset float or sequence of float

Vertical offset(s) for the subplot labels. If a single float, the same offset is applied to all subplots. Default is 0.015.

0.015

Returns:

Type Description
None

This function does not return any value.

Source code in allinpy/plotting/plot_utils.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def label_subplots(
    f: Figure,
    texts: Sequence[str],
    x_offset: Union[float, Sequence[float]] = -0.07,
    y_offset: Union[float, Sequence[float]] = 0.015,
) -> None:
    """This function labels the subplots.

    Obtained from: https://stackoverflow.com/questions/52286497/
    matplotlib-label-subplots-of-different-sizes-the-exact-same-distance-from-corner

    Parameters
    ----------
    f : matplotlib.figure.Figure
        Matplotlib figure handle containing the subplots.
    texts : sequence of str
        List of labels for each subplot (e.g., ["A", "B", "C"]).
    x_offset : float or sequence of float, optional
        Horizontal offset(s) for the subplot labels.
        If a single float, the same offset is applied to all subplots.
        Default is -0.07.
    y_offset : float or sequence of float, optional
        Vertical offset(s) for the subplot labels.
        If a single float, the same offset is applied to all subplots.
        Default is 0.015.

    Returns
    -------
    None
        This function does not return any value.
    """

    # Get axes
    axes = f.get_axes()

    if isinstance(x_offset, float):
        x_offset = np.repeat(x_offset, len(axes))

    if isinstance(y_offset, float):
        y_offset = np.repeat(y_offset, len(axes))

    # Cycle over subplots and place labels
    axis_counter = 0
    for a, l in zip(axes, texts):
        x = a.get_position().x0
        y = a.get_position().y1
        f.text(x - x_offset[axis_counter], y + y_offset[axis_counter], l, size=12)
        axis_counter += 1