Created
October 17, 2020 13:49
-
-
Save andrashann/3ff77487786de4fe8eeb45b3839b5a49 to your computer and use it in GitHub Desktop.
Revisions
-
andrashann created this gist
Oct 17, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ import matplotlib import numpy as np import matplotlib.pyplot as plt #https://scipy-cookbook.readthedocs.io/items/Matplotlib_ColormapTransformations.html def cmap_map(function, cmap): """ Applies function (which should operate on vectors of shape 3: [r, g, b]), on colormap cmap. This routine will break any discontinuous points in a colormap. """ cdict = cmap._segmentdata step_dict = {} # Firt get the list of points where the segments start or end for key in ('red', 'green', 'blue'): step_dict[key] = list(map(lambda x: x[0], cdict[key])) step_list = sum(step_dict.values(), []) step_list = np.array(list(set(step_list))) # Then compute the LUT, and apply the function to the LUT reduced_cmap = lambda step : np.array(cmap(step)[0:3]) old_LUT = np.array(list(map(reduced_cmap, step_list))) new_LUT = np.array(list(map(function, old_LUT))) # Now try to make a minimal segment definition of the new LUT cdict = {} for i, key in enumerate(['red','green','blue']): this_cdict = {} for j, step in enumerate(step_list): if step in step_dict[key]: this_cdict[step] = new_LUT[j, i] elif new_LUT[j,i] != old_LUT[j, i]: this_cdict[step] = new_LUT[j, i] colorvector = list(map(lambda x: x + (x[1], ), this_cdict.items())) colorvector.sort() cdict[key] = colorvector return matplotlib.colors.LinearSegmentedColormap('colormap',cdict,1024)