Skip to content

Instantly share code, notes, and snippets.

@lschwetlick
Created March 17, 2021 15:24
Show Gist options
  • Save lschwetlick/4f03bd162602daaab656603fb7150caa to your computer and use it in GitHub Desktop.
Save lschwetlick/4f03bd162602daaab656603fb7150caa to your computer and use it in GitHub Desktop.
New matplotlib cmap from color list
import matplotlib.colors
def colors2cmap(*args, name=None):
"""Create a colormap from a list of given colors.
Parameters:
*args: Arbitrary number of colors (Named color, HEX or RGB).
name (str): Name with which the colormap is registered.
Returns:
LinearSegmentedColormap.
Examples:
>>> colors2cmap('darkorange', 'white', 'darkgreen', name='test')
From https://github.com/atmtools/typhon/blob/master/typhon/plots/common.py
"""
if len(args) < 2:
raise Exception("Give at least two colors.")
cmap_data = [matplotlib.colors.to_hex(c) for c in args]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(name, cmap_data)
plt.register_cmap(name, cmap)
return cmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment