Created
March 17, 2021 15:24
-
-
Save lschwetlick/4f03bd162602daaab656603fb7150caa to your computer and use it in GitHub Desktop.
New matplotlib cmap from color list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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