fastplotlib.utils¶

calculate_figure_shape(n_subplots)[source]¶

Returns (n_rows, n_cols) from given number of subplots n_subplots

Return type:

tuple[int, int]

get_cmap(name, alpha=1.0)[source]¶

Get a colormap as numpy array

Parameters:
  • name (str) – name of colormap

  • alpha (float) – alpha, 0.0 - 1.0

Returns:

[n_colors, 4], i.e. [n_colors, RGBA]

Return type:

np.ndarray

make_colors(n_colors, cmap, alpha=1.0)[source]¶

Get colors from a colormap. The returned colors are uniformly spaced, except for qualitative colormaps where they are returned subsequently.

Parameters:
  • n_colors (int) – number of colors to get

  • cmap (str) – name of colormap

  • alpha (float, default 1.0) – alpha value

Returns:

shape is [n_colors, 4], where the last dimension is RGBA

Return type:

np.ndarray

make_colors_dict(labels, cmap, **kwargs)[source]¶

Get a dict for mapping labels onto colors.

Parameters:
  • labels (Iterable[Any]) – labels for creating a colormap. Order is maintained if it is a list of unique elements.

  • cmap (str) – name of colormap

  • **kwargs – passed to make_colors()

Returns:

keys are labels, values are colors

Return type:

OrderedDict

Examples

from fastplotlib.utils import get_colors_dict

labels = ["l1", "l2", "l3"]
labels_cmap = get_colors_dict(labels, cmap="tab10")

# illustration of what the `labels_cmap` dict would look like:
# keep in mind that the tab10 cmap was chosen here

{
    "l1": <RGBA array for the blue 'tab10' color>,
    "l2": <RGBA array for the orange 'tab10' color>,
    "l3": <RGBA array for the green 'tab10' color>,
}

# another example with a non-qualitative cmap
labels_cmap_seismic = get_colors_dict(labels, cmap="bwr")

{
    "l1": <RGBA array for the blue 'bwr' color>,
    "l2": <RGBA array for the white 'bwr' color>,
    "l3": <RGBA array for the red 'bwr' color>,
}
make_pygfx_colors(colors, n_colors)[source]¶

Parse and make colors array using pyfx.Color

Parameters:
  • colors (str, list, tuple, or np.ndarray) – pygfx parseable color

  • n_colors (int) – number of repeats of the color

Returns:

shape is [n_colors, 4], i.e. [n_colors, RGBA]

Return type:

np.ndarray

normalize_min_max(a)[source]¶

normalize an array between 0 - 1

parse_cmap_values(n_colors, cmap_name, cmap_values=None)[source]¶
Parameters:
  • n_colors (int) – number of graphics in collection

  • cmap_name (str) – colormap name

  • cmap_values (np.ndarray | List[int | float], optional) – cmap values

Return type:

ndarray

quick_min_max(data)[source]¶

Adapted from pyqtgraph.ImageView. Estimate the min/max values of data by subsampling.

Parameters:

data (np.ndarray or array-like with min and max attributes)

Returns:

(min, max)

Return type:

(float, float)