Color functions#
Get, show, print color palettes
This module was written by Matthias Cuntz while at Institut National de Recherche pour l’Agriculture, l’Alimentation et l’Environnement (INRAE), Nancy, France.
- copyright:
Copyright 2021- Matthias Cuntz, see AUTHORS.rst for details.
- license:
MIT License, see LICENSE for details.
The following functions are provided:
|
Register new named colors with Matplotlib and return named color(s) |
|
Print the known named colors |
|
Get colors of defined palettes or continuous colormaps |
|
Print the known color palettes and continuous colormaps |
|
Show the known color palettes and continuous colormaps |
- History
Written Nov 2021, Matthias Cuntz
Change order of color maps, Dec 2021, Matthias Cuntz
More consistent docstrings, Jan 2022, Matthias Cuntz
Added ‘order’ keyword, Feb 2022, Matthias Cuntz
‘get_color’, Mar 2022, Matthias Cuntz
‘cname’ can be list of names in ‘get_color’, Apr 2022, Matthias Cuntz
Register ufz colors only once, Apr 2022, Matthias Cuntz
Added print_colors, Apr 2022, Matthias Cuntz
Use matplotlib.colormaps.get_cmap instead of deprecated matplotlib.cm.get_cmap, Dec 2022, Matthias Cuntz
Use matplotlib.colormaps[name] instead of matplotlib.colormaps.get_cmap(name) to work with matplotlib < v3.6, Jan 2023, Matthias Cuntz
Do not set_bad() for sron palettes, Jan 2023, Matthias Cuntz
Allow ncol=1 in get_cmap, Jun 2023, Matthias Cuntz
Added IPCC colours, Feb 2024, Matthias Cuntz
- get_cmap(palette, ncol=0, offset=0, upper=1, reverse=False, grey=False, order=None, as_cmap=False)[source]#
Get colors of defined palettes or continuous colormaps
- Parameters:
palette (str) – Name of color palette or continuous colormap
ncol (int, optional) – Number of desired colors. If 0, all colors defined by the specific palette will be returned. 256 colors will be chosen for continuous colormaps. If > 0, existing color palettes will be subsampled to ncol colors. ncol colors will be produced from continuous colormaps.
offset (float (0-1), optional) – Bottom fraction to exclude for subsample or continuous colormaps
upper (float (0-1), optional) – Upper most fraction to include for subsample or continuous colormaps
reverse (bool, optional) – Reverse colormap if True (default: False). This can also be achieved by adding ‘_r’ to the palette name. Palettes that end with ‘_r’ will not be reversed.
grey (bool, optional) – Return grey equivalent colors if True (default: False).
order (str, optional) – Order colors by ‘hue’, ‘saturation’, or ‘value’. This is done before reverse and grey (default: order from color sources).
as_cmap (bool, optional) – If True, returns matplotlib.colors.Colormap instead of list of RGB tuples
- Returns:
List of RGB tuples or
matplotlib.colors.Colormap
- Return type:
Notes
get_cmap always returns a list of RGB tuples or a Matplotlib ListedColormap. You can use the ncol, offset, and upper keywords to subsample colormaps. To get smoothly-varying colormaps, you can use the method
matplotlib.colors.LinearSegmentedColormap.from_list()
cols = get_cmap(‘sron_ylorbr’)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(cols)
You can get the color tuples from a LinearSegmentedColormap like this
colors = [ cmap(i) for i in range(cmap.N) ]
Examples
cols = get_cmap('mathematica_dark_rainbow_256', 15)
- get_color(cname='')[source]#
Register new named colors with Matplotlib and return named color(s)
Registers the named colors given in ufz_palettes. Optionally returns the value(s) of the named color(s), which can be any color name known to Matplotlib.
Examples
col1 = get_color('ufz:blue') col2 = get_color('xkcd:blue') cols = get_color(['blue', 'red'])
- print_colors(collection='')[source]#
Print the known named colors
- Parameters:
collection (str or list of strings, optional) – Name(s) of color collection(s). Known collections are ‘base’, ‘tableau’, ‘ufz’, ‘css’, and ‘xkcd’.
- Returns:
Prints list of known named colors to console.
- Return type:
None
Examples
print_colors()
- print_palettes(collection='')[source]#
Print the known color palettes and continuous colormaps
- Parameters:
collection (str or list of strings, optional) – Name(s) of color palette collection(s). Known collections are ‘pyjams’, ‘sron’, ‘sron2012’, ‘mathematica’, ‘oregon’, ‘ipcc’, ‘ncl’, ‘matplotlib’, and ‘brewer’.
- Returns:
Prints list of known color palettes and continuous colormaps to console.
- Return type:
None
Examples
print_palettes()
- show_palettes(outfile='', collection='')[source]#
Show the known color palettes and continuous colormaps
- Parameters:
outfile (str, optional) – Output file name. Output type will be determined from file suffix.
collection (str or list of strings, optional) – Name(s) of color palette collection(s). All palettes will be shown if collection is empty or ‘all’. Known collections are: ‘pyjams’, ‘sron’, ‘sron2012’, ‘mathematica’, ‘oregon’, ‘ipcc’, ‘ncl’, ‘matplotlib’, and ‘brewer’.
- Returns:
Plots known color palettes and continuous colormaps in file or on plotting window.
- Return type:
None
Examples
show_palettes(outfile='pyjams_cmaps.pdf', collection=['mathematica', 'matplotlib'])