Skip to content

Instantly share code, notes, and snippets.

@gebelo
Last active March 10, 2018 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gebelo/c8141bdcff9c9559e946afa659b26354 to your computer and use it in GitHub Desktop.
Save gebelo/c8141bdcff9c9559e946afa659b26354 to your computer and use it in GitHub Desktop.
from matplotlib.colors import LinearSegmentedColormap
bins = [0,1]
# Maps values to a bin.
# The mapped values must start at 0 and end at 1.
def bin_mapping(x):
for idx, bound in enumerate(bins):
if x < bound:
return idx / (len(bins) - 1.0)
# Create the list of bin labels and the list of colors
# corresponding to each bin
bin_labels = [idx / (len(bins) - 1.0) for idx in range(len(bins))]
color_list = ['#edf8fb', '#b30000']
# Create the custom color map
cmap = LinearSegmentedColormap.from_list('mycmap',
[(lbl, color) for lbl, color in zip(bin_labels, color_list)])
for year in range(1990, 2017):
plot_col="RATIO"+str(year)
counties['Bin_Lbl'] = counties[plot_col].apply(bin_mapping)
mpl.pyplot.savefig(str(year) + 'map.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment