nbtools.colors

Functions/classes relative to colors.

Functions

make_boundary_norm(ncolors, boundaries) Normalisation of colormaps for “discrete” mapping.
make_percentile_cmap(array, perc) Returns colormaps limits using percentiles.
subspan_default_cmap(cmapname, nbcol) Function that allows to generate a colormap containing nbcol colors

Classes

FancyCmap(input_cmap[, smooth, reverse, ...]) Class for generating fancy colormaps.
class nbtools.colors.FancyCmap(input_cmap, smooth=False, reverse=False, list_offset=None)[source]

Class for generating fancy colormaps.

It creates the attributes offset (containing the linear mapping segments) and col (containing the associated colors).

Parameters:
  • input_cmap (str|int|numpy.array) – Colormap to use. Can be a string (the name of the .svg file to read), a list of named colors or a 3xN/Nx3 numpy.array (percentage of RGB values)
  • smooth (bool) – If the input_cmap argument is a list or an array, define whether the colormap is smoothed (i.e gradient type colormap) or not.
  • reverse (bool) – Defines whether the colors should be reversed.
  • list_offset (list) – List of values containing mapping segments. Only used for user defined colormaps. If None, regular offseting is used.
generate_cbticks(cbar)[source]

Writes colorbar ticks for discrete colormaps.

It writes a label at each edges of the colorbar.

Parameters:cbar (matplotlib.colorbar.Colorbar) – The colorbar object
Returns:None
makecmap(nbcol=256)[source]

Create the colormap instance.

The output of this function will be provided in cmap argument of the matplotlib.pyplot.contourf() or similar functions.

Parameters:
  • nbcol (int) – Number of colors in the colormap
  • reverse (bool) – defines whether the colormap should be reversed or not (equivalent to the _r for instance)
Returns:

A colormap object

Return type:

matplotlib.colors.LinearSegmentedColormap

nbtools.colors.make_boundary_norm(ncolors, boundaries)[source]

Normalisation of colormaps for “discrete” mapping. If

boundary[i] <= value < boundary[i+1]

then the data is mapped to color(i) (cf matplotlib.colors.BoundaryNorm documentation).

Parameters:
  • ncolors (int) – Number of colors to display on the colormap
  • boundaries (numpy.array) – Array containing the colorbar boundaries (i.e the values of the colorbar edges). Must have (ncolors+1) elements.

Warning

When using the output norm in a pcolormesh/imshow function, the colormap that is used must have ncolors colors. This is achieved either by defining your own colormap with the FancyCmap class or by using the subspan_default_cmap() function on Matplotlib default colormaps.

Return type:matplotlib.colors.Normalize
Returns:A norm instance that contains the discrete mapping
nbtools.colors.make_percentile_cmap(array, perc)[source]

Returns colormaps limits using percentiles. The minimum color value.

Parameters:array (numpy.array) – Input array

:param int perc : Percentile (in percentage)

Returns:A tuple containing the lower and upper colorbar limits associated with the array percentiles.
Return type:tuple
nbtools.colors.subspan_default_cmap(cmapname, nbcol)[source]

Function that allows to generate a colormap containing nbcol colors from the default colormap data mapping dictionnaries (cf the datad dictionnary in the matplotlib._cm module)

Parameters:
  • cmapname (str) – Name of the matplotlib colormap
  • nbcol (int) – Number of colors to extract from the cmapname colormap
Returns:

A colormap object

Return type:

matplotlib.colors.LinearSegmentedColormap