Skip to content

subplots

Wrapper for matplotlib functions for subplots.

hide_empty_axes(axs=None)

Hide empty axes.

Loop over all axes and hide empty ones.

Parameters:

  • axs (mpl.axes.Axes or list of, default: None ) –

    Specify matplotlib.axes.Axes to check for empty state. Default use all of current figure.

Source code in src/prettypyplot/subplots.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def hide_empty_axes(axs=None):
    """Hide empty axes.

    Loop over all axes and hide empty ones.

    Parameters
    ----------
    axs : mpl.axes.Axes or list of
        Specify [matplotlib.axes.Axes][] to check for empty state. Default use
        all of current figure.

    """
    # check for single axes
    axs = tools.get_axes(axs)

    # loop over all axes and hide empty ones
    for ax in axs:
        if _is_empty_axes(ax):
            ax.axis('off')

    # in case of shared axes, activate outer tick labels
    _activate_outer_ticks(axs)

label_outer(axs=None)

Only show outer labels and tick labels.

This checks for outest visible axes only. Works only with single Gridspec.

Parameters:

  • axs (mpl.axes.AxesSubplot or list of, default: None ) –

    Specify matplotlib.axes.Axes to check for labeling only outer. Default use all of current figure.

Source code in src/prettypyplot/subplots.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def label_outer(axs=None):
    """Only show outer labels and tick labels.

    This checks for outest visible axes only. Works only with single Gridspec.

    Parameters
    ----------
    axs : mpl.axes.AxesSubplot or list of
        Specify [matplotlib.axes.Axes][] to check for labeling only outer.
        Default use all of current figure.

    """
    # check for single axes
    if axs is not None:
        axs = tools.get_axes(axs)
        if not all((_is_subplot_axes(arg) for arg in axs)):
            raise TypeError(
                'axs needs to be of type matplotlib.axes.AxesSuplot.',
            )
    else:
        axs = [ax for ax in plt.gcf().get_axes() if _is_subplot_axes(ax)]

    for ax in axs:
        ss = ax.get_subplotspec()
        if hasattr(ss, 'is_last_row'):  # pragma: no cover # noqa: WPS421
            # for mpl >= 3.4
            lastrow = ss.is_last_row()
            firstcol = ss.is_first_col()
        elif hasattr(ax, 'is_last_row'):  # pragma: no cover # noqa: WPS421
            lastrow = ax.is_last_row()
            firstcol = ax.is_first_col()
        else:
            raise TypeError(f'{ax!r} is not a valid axes.')

        # check if axes below, left is hidden
        left_empty, bottom_empty = _is_outer_hidden(axs, ax)
        _label_outer(ax, lastrow or bottom_empty, firstcol or left_empty)

subplot_labels(*, fig=None, xlabel=None, ylabel=None)

Add global labels for subplots.

This method adds shared x- and y-labels for a grid of subplots. These can be created by, e.g. fig, axs = plt.subplots(...).

Parameters:

  • fig (matplotlib figure, default: None ) –

    If None the current figure will be used instead.

  • xlabel (str, default: None ) –

    String of x label.

  • ylabel (str, default: None ) –

    String of y label.

Source code in src/prettypyplot/subplots.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def subplot_labels(*, fig=None, xlabel=None, ylabel=None):
    """Add global labels for subplots.

    This method adds shared x- and y-labels for a grid of subplots. These can
    be created by, e.g. `fig, axs = plt.subplots(...)`.

    Parameters
    ----------
    fig : matplotlib figure, optional
        If `None` the current figure will be used instead.
    xlabel : str, optional
        String of x label.
    ylabel : str, optional
        String of y label.

    """
    # if no label passed, nothing to do
    if xlabel is None and ylabel is None:
        return

    # get active axes to restore it later on
    ca = plt.gca()

    if fig is None:
        fig = plt.gcf()

    _subplot_labels(fig, xlabel, ylabel)

    # reset current axes
    plt.sca(ca)