1

In relation to the accepted response to this question:

What if I wanted to use 3 or more different, custom colors? Is it an easy edit to LinearSegmentedColormap?

This works for 2:

import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

shpfile = 'cb_2015_us_county_20m.shp'
c = gpd.read_file(shpfile)
c = c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075',
                           '26125','26163','26099','26115','26065'])]

c['color'] = np.zeros(len(c))
# 23 is index for Washtenaw county and 1992 is index for Wayne county
c.ix[23, 'color'] = 1.0
c.ix[1992, 'color'] = 1.0

# create simple linear colormap that maps grey to blue
cmap = LinearSegmentedColormap.from_list(
    'mycmap', [(0, 'grey'), (1, 'blue')])

c.plot(column='color', cmap=cmap)

For 3, I tried:

cmap = LinearSegmentedColormap.from_list(
    'mycmap', [(0, 'grey'), (0.5, 'red'), (1, 'blue')])

Thanks in advance!

Community
  • 1
  • 1
Dance Party2
  • 7,214
  • 17
  • 59
  • 106
  • 1
    I don't see why this wouldn't work in python 3? What went wrong? Don't forget that you have to update the values in `c['color']` if you want counties to start appearing as different colors. – lanery Aug 11 '16 at 19:11
  • Sorry, I didn't have all values represented in my data frame (only had 0.5 and 1 with no zeros). – Dance Party2 Aug 11 '16 at 19:19
  • So if I had 0 set to grey, 0.5 to red, and 1 to blue and only had 0.5 and 1 values in my color column, it plots rows with 0.5 as grey instead of red, I'm assuming because that's the smallest number in the column. Is there a way to make this more absolute so 0.5 always plots as red no matter what range of values are present in the color column? – Dance Party2 Aug 11 '16 at 19:24
  • I just used a series of if-elifs to outline different scenarios. – Dance Party2 Aug 11 '16 at 19:45
  • Ok, sorry I didn't get to your question in time. Using custom colormaps can get really annoying once you start trying to custom color more than a couple objects. If I had known you wanted a different color for each county from the get-go, I probably would have suggested using something other than `LinearSegmentedColormap` – lanery Aug 11 '16 at 19:50
  • 1
    No problem. The if-elif solution works perfectly fine for me. Thanks! – Dance Party2 Aug 11 '16 at 19:51
  • @Ianery: what would you recommend instead of LinearSegmentedColormap for more then 2 custom colors? – Dance Party2 Aug 18 '16 at 18:08
  • [`ListedColormap`](http://matplotlib.org/api/colors_api.html#matplotlib.colors.ListedColormap) off the top of my head. See also this example http://matplotlib.org/examples/api/colorbar_only.html – lanery Aug 18 '16 at 18:20
  • @Ianery what if I want a range of colors (spectrum as with a chloropleth) but if the value is 0.00000 or less, make it 'white'? I currently have a map that works except that the bottom 10% of values are treated as zeros and thus the corresponding counties are white. I can post a separate question if needed. – Dance Party2 Aug 18 '16 at 18:38
  • 1
    Check out `set_over` and `set_under`, and maybe `set_bad` as well. This method does exactly what I think you're looking for. There are examples out there (e.g. http://stackoverflow.com/questions/22548813/python-color-map-but-with-all-zero-values-mapped-to-black), but if you still have trouble you can either edit this question or post a new one. Good luck! – lanery Aug 18 '16 at 19:14
  • @Ianery, you're the best! – Dance Party2 Aug 19 '16 at 19:22

0 Answers0