Skip to content

Instantly share code, notes, and snippets.

@andrashann
Created October 17, 2020 13:49
Show Gist options
  • Save andrashann/3ff77487786de4fe8eeb45b3839b5a49 to your computer and use it in GitHub Desktop.
Save andrashann/3ff77487786de4fe8eeb45b3839b5a49 to your computer and use it in GitHub Desktop.

Revisions

  1. andrashann created this gist Oct 17, 2020.
    35 changes: 35 additions & 0 deletions cmap_map.py
    Original 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)
    1,310 changes: 1,310 additions & 0 deletions new_york_distancing_space.ipynb
    1,310 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.