Skip to content

myco.vis

Visualization tools for ndarray data and tensorflow modeling.

get_styles()

Lists the available plotting styles.

Returns:

Type Description
list

list of style names to pass to set_style()

Source code in myco/vis.py
29
30
31
32
33
34
35
36
37
38
39
def get_styles() -> list:
    """Lists the available plotting styles.

    Args:
        None.

    Returns:
        list of style names to pass to `set_style()`
    """
    styles = mpl.style.available
    return styles

plot_3d_array(array, vmin=None, vmax=None, labels=None, title=None, colorbar=True, cmap=DEFAULT_CMAP, style=DEFAULT_STYLE, **kwargs)

Creates a square set of subplots for a 3d numpy array.

Parameters:

Name Type Description Default
array np.ndarray

a [height, width, nbands] numpy array. Will become nbands subplots.

required
vmin _number

set the lower range of the colorbar to this value.

None
vmax _number

set the upper range of the colorbar to this value.

None
labels list

nbands-length list of band labels to apply to each subplot.

None
title str

the full figure title.

None
colorbar bool

add a labeled colorbar to each subplot.

True
cmap mpl.colors.LinearSegmentedColormap

the colormap to apply to each band.

DEFAULT_CMAP
style str

the matplotlib plot style to apply.

DEFAULT_STYLE
**kwargs

keyword arguments to pass to plt.subplots()

{}

Returns:

Type Description
tuple

(fig, axs) matplotlib subplot figure and axes. run fig.show() to display.

Source code in myco/vis.py
 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def plot_3d_array(
    array: np.ndarray,
    vmin: _number = None,
    vmax: _number = None,
    labels: list = None,
    title: "str" = None,
    colorbar: bool = True,
    cmap: mpl.colors.LinearSegmentedColormap = DEFAULT_CMAP,
    style: "str" = DEFAULT_STYLE,
    **kwargs,
) -> tuple:
    """Creates a square set of subplots for a 3d numpy array.

    Args:
        array: a [height, width, nbands] numpy array. Will become `nbands` subplots.
        vmin: set the lower range of the colorbar to this value.
        vmax: set the upper range of the colorbar to this value.
        labels: `nbands`-length list of band labels to apply to each subplot.
        title: the full figure title.
        colorbar: add a labeled colorbar to each subplot.
        cmap: the colormap to apply to each band.
        style: the matplotlib plot style to apply.
        **kwargs: keyword arguments to pass to `plt.subplots()`

    Returns:
        (fig, axs) matplotlib subplot figure and axes. run `fig.show()` to display.
    """
    height, width, nbands = array.shape

    if style is not None:
        set_style(style)

    if labels is None:
        n_digits = _count_digits(nbands)
        labels = [f"band_{i+1:0{n_digits}d}" for i in range(nbands)]

    # create the subplots
    figx, figy = _square_factor(nbands)
    fig, axs = plt.subplots(figx, figy, **kwargs)

    # handle single/multiband differences
    if nbands == 1:
        axs = [axs]
    else:
        axs = axs.reshape(nbands)

    # plot each array
    for idx in range(nbands):
        ax = axs[idx]
        data = array[:, :, idx]
        label = labels[idx]

        im = ax.imshow(data, cmap=cmap, vmin=vmin, vmax=vmax)
        ax.set_title(label, fontsize="x-small")
        ax.axis("off")
        if colorbar:
            cbar = plt.colorbar(im, ax=ax)
            cbar.ax.tick_params(labelsize="xx-small")
            cbar.outline.set_linewidth(0.5)

    fig.suptitle(title, fontsize="x-small")
    fig.tight_layout()

    if nbands == 1:
        axs = axs[0]

    return fig, axs

set_style(style)

Sets a series of plot style parameters.

Parameters:

Name Type Description Default
style str

str of the style name to use. from plot.get_styles()

required

Returns:

Type Description
None

None. Updates matplotlib.rcParams

Source code in myco/vis.py
17
18
19
20
21
22
23
24
25
26
def set_style(style: str) -> None:
    """Sets a series of plot style parameters.

    Args:
        style: str of the style name to use. from `plot.get_styles()`

    Returns:
        None. Updates matplotlib.rcParams
    """
    mpl.style.use(style)