Naturalcolors tutorial¶
import numpy as np
import importlib
import matplotlib as mpl
import naturalcolors.colorpalette as ncp
Documentation¶
Documentation on how to create a colormap from a list of colors (a listed colormap) or by specifying anchor points between which the RGB(A) colors are interpolated (a linear segmented colormap) can be found here:
https://matplotlib.org/3.1.0/tutorials/colors/colormap-manipulation.html
The naturalcolors colormap¶
Naturalcolors provides wrapper functions to create a colormap from a list of colors in a json file. The colormap is generated by calling the naturalcolors() function from the colorpalette submodule.
natcmap_list,natcmap_linseg = ncp.naturalcolors()
The resulting listed and linear segmented colormaps can be visualized either by a colorbar or a color circle
ncp.drawColorBar(natcmap_linseg)
Draw a color circle with the naturalcolors colormap
ncp.drawColorCircle(natcmap_linseg, 15, 2000)
Custom colormaps¶
You can create your own custom colormaps directly from a list of colors or by loading them from a json file using the load_colors function from the colorpalette submodule.
The json file should be structures as a list of rgb(a) colors:
[[234,33,59],
[237,65,55],
[239,102,58],
...]
colors = [[0.31, 0.45, 0.56],[0.9,0.9,0.9], [0.75, 0.51, 0.38]]
cmap_list,cmap_linseg = ncp.make_colormap(colors, 'BlueWhiteOrange')
ncp.drawColorCircle(cmap_linseg, area=500)
Registered colormaps¶
Registered colormaps can also be called directly from their name
ncp.drawColorCircle('Blues', area=500)
A registered colormap can be accessed from its name by the following equivalent commands:
# from the cmap submodule
mpl.cm.get_cmap('Blues')
# from the pyplot submodule
mpl.pyplot.get_cmap('Blues')
<matplotlib.colors.LinearSegmentedColormap at 0x7f53bc975278>
A list of n colors can be extracted from a colormap using the get_colors function of the colorpalette submodule (calls cmap(np.linspace(0,1,n) internally). Using the flag scramble=True (default=False) rearranges the color array as [[first rgb],[last rgb],[second rgb],...]]
ncp.get_colors('Blues', 5, scramble=True)
array([[0.96862745, 0.98431373, 1. , 1. ],
[0.03137255, 0.18823529, 0.41960784, 1. ],
[0.77524029, 0.85830065, 0.9368243 , 1. ],
[0.12710496, 0.44018454, 0.70749712, 1. ],
[0.41708574, 0.68063053, 0.83823145, 1. ]])