Instantly share code, notes, and snippets.

# andrashann/cmap_map.py

Created October 17, 2020 13:49
Show Gist options
• Save andrashann/3ff77487786de4fe8eeb45b3839b5a49 to your computer and use it in GitHub Desktop.
Companion notebook to my article about population density and social distancing
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 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)
Display the source blob
Display the rendered blob
Raw
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
\n", "\n", "\n", "
ntacodeshape_areacounty_fipsntanameshape_lengboro_nameboro_codegeometry
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0BK8854005019.048047Borough Park39247.2278309Brooklyn3MULTIPOLYGON (((990897.900 169268.121, 990588....
1QN5152488277.5915081Murray Hill33266.9048721Queens4MULTIPOLYGON (((1038593.459 221913.355, 103936...
2QN2719726845.5925081East Elmhurst19816.7121316Queens4MULTIPOLYGON (((1022728.275 217530.808, 102305...
3BK9514522603.5915047Erasmus18756.7018646Brooklyn3MULTIPOLYGON (((998608.334 171942.436, 998339....
4QN0722887772.8087081Hollis20976.3355996Queens4MULTIPOLYGON (((1051539.660 201007.013, 105192...
5BK2529991967.283047Homecrest27514.0229182Brooklyn3MULTIPOLYGON (((995746.857 161665.390, 995833....
7BX3310732452.1794005Longwood18982.1296554Bronx2MULTIPOLYGON (((1013354.947 240318.698, 101334...
8BK4335799574.385047Midwood27996.5906116Brooklyn3MULTIPOLYGON (((995312.051 168409.655, 995365....
9QN0877412748.1543081St. Albans45401.3170041Queens4MULTIPOLYGON (((1052996.114 196307.361, 105308...
BoroughYearFIPS County CodeNTA CodeNTA NamePopulation
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
233Brooklyn201047BK09Brooklyn Heights-Cobble Hill22887
38Brooklyn200047BK09Brooklyn Heights-Cobble Hill22548
40Brooklyn200047BK19Brighton Beach35244