def create_color_mapper(mapper_type, c_min, c_max, cmap="YlGnBu", cmap_res=8, cmap_r=True): """ create the colormapper mapper_type = "log" or "linear" """ # define the color palette palette = brewer[cmap][cmap_res] # reverese the color palette if desired if cmap_r == True: palette = palette[::-1] #Instantiate LogColorMapper that maps numbers in a range, into a sequence of colors. # Create the specified color mapper if mapper_type == "log": color_mapper = LogColorMapper(palette=palette, low=c_min, high=c_max) elif mapper_type == "linear": color_mapper = LinearColorMapper(palette=palette, low=c_min, high=c_max) else: raise ValueError("mapper_type must be specified as either 'log' or 'linear'") # set the nan_color of the color mapper color_mapper.nan_color = "#f0f0f0" return color_mapper
def make_map(d, map_width): rev_dict = {} path = '/Users/a6002538/Downloads/results-20190429-111727.csv' with open(path, 'r') as read_obj: reader = csv.DictReader(read_obj) for row in reader: rev_dict[row['fips']] = float(row['rev_weighted']) for key in rev_dict.keys(): if rev_dict[key] > 1: print(key) p = figure(title='Dollars Spent Per Person', width=map_width, height=int(round(4 / 5 * map_width))) palette = Blues8 palette.reverse() #color_mapper = LinearColorMapper(palette=palette) color_mapper = LogColorMapper(palette=palette) color_mapper.nan_color = 'gray' _data = [] for key in d.keys(): _data.append((rev_dict.get(key, 0), d[key]['x'], d[key]['y'])) _data = sorted(_data) data = [x[0] for x in _data] xs = [x[1] for x in _data] ys = [x[2] for x in _data] #legends = make_legends(data, palette) #fill_colors, legends = log_mapper(data, palette) fill_colors, legends = linear_mapper(data, palette) source = ColumnDataSource(data=dict( x=xs, y=ys, data=data, legend=legends, fill_colors=fill_colors)) p.patches('x', 'y', source=source, fill_color='fill_colors', fill_alpha=0.7, line_color='black', line_width=.2, legend='legend') p.legend.location = "bottom_right" return p
def make_map(d, map_width): rev_dict = {} with open('/Users/a6002538/Downloads/results-20190426-163009.csv', 'r') as read_obj: reader = csv.DictReader(read_obj) for row in reader: rev_dict[row['fips']] = float(row['rev']) p = figure(width=map_width, height=int(round(4 / 5 * map_width))) palette = Blues8 palette.reverse() #color_mapper = LinearColorMapper(palette=palette) color_mapper = LogColorMapper(palette=palette) color_mapper.nan_color = 'gray' _data = [] for key in d.keys(): _data.append((rev_dict.get(key, 0), d[key]['x'], d[key]['y'])) _data = sorted(_data) data = [x[0] for x in _data] xs = [x[1] for x in _data] ys = [x[2] for x in _data] #legends = make_legends(data, palette) log_list = [math.log(x) if x > 0 else 0 for x in data] perc = [1 / len(palette) * x for x in range(1, len(palette) + 1)] q = np.quantile(log_list, perc) ls = [] prev = 0 for i in q: n = '{0:,}'.format(round(math.exp(i))) ls.append('{p}:{n}'.format(p=prev, n=n)) prev = n ls.append('>') fill_colors = [] legends = [] for i in data: if i == 0 or math.log(i) <= q[0]: fill_colors.append(palette[0]) legends.append(ls[0]) elif math.log(i) <= q[1]: fill_colors.append(palette[1]) legends.append(ls[1]) elif math.log(i) <= q[2]: fill_colors.append(palette[2]) legends.append(ls[2]) elif math.log(i) <= q[3]: fill_colors.append(palette[3]) legends.append(ls[3]) elif math.log(i) <= q[4]: legends.append(ls[4]) fill_colors.append(palette[4]) elif math.log(i) <= q[5]: fill_colors.append(palette[5]) legends.append(ls[5]) elif math.log(i) <= q[6]: fill_colors.append('orange') legends.append(ls[6]) elif math.log(i) <= q[7]: fill_colors.append('red') legends.append(ls[7]) else: fill_colors.append('orange') legends.append(ls[8]) source = ColumnDataSource(data=dict( x=xs, y=ys, data=data, legend=legends, fill_colors=fill_colors)) p.patches('x', 'y', source=source, fill_color='fill_colors', fill_alpha=0.7, line_color='black', line_width=.2, legend='legend') p.legend.location = "bottom_right" return p