def viz_zone_stat(input_shapefile,poly,uid): try: os.remove('ndvi_stats.html') except OSError: pass palette.reverse() output_notebook() output_file('ndvi_stats1.html') props = OrderedDict() with fiona.open(input_shapefile, 'r') as polys: for i in polys: prop = OrderedDict() prop[i['properties'].get(uid)] = {} prop[i['properties'].get(uid)].update({ stat : val for stat , val in i['properties'].items()}) props.update(prop) px = [] py = [] for i in poly: x,y = i.exterior.xy x=x.tolist() y=y.tolist() px.append(x) py.append(y) polyID = [] mndvi = [] std_ndvi= [] color_mapper = LogColorMapper(palette=palette) for key in props: polyID.append(props[key][uid]) mndvi.append(props[key]['mean']) std_ndvi.append(props[key]['std']) source = ColumnDataSource(data=OrderedDict( tid=polyID,mndvi=mndvi,stdndvi=std_ndvi,x=px,y=py)) TOOLS = "pan,wheel_zoom,reset,hover,save" TOOLTIPS=[ ((uid), "@tid"), ("Mean NDVI", "@mndvi"), ("Std. NDVI", "@stdndvi")] p = figure(plot_width=500, plot_height=500,title="Zonal Statistics") #p.hover.point_policy = "follow_mouse" "fill color", "$color[hex, swatch]:fill_color" plots = p.patches('x', 'y', source=source, fill_color={'field': 'mndvi', 'transform': color_mapper}, fill_alpha=0.7, line_color="black", line_width=0.5) p.add_tools(HoverTool(tooltips=TOOLTIPS, mode='mouse')) show(p)
def plot_bokeh_intensity_map(self, spatial_entity_to_coordinates, spatial_entity_to_values): entity_xs = [] entity_ys = [] entity_names = [] entity_rates = [] for name, coords in spatial_entity_to_coordinates.items(): xs = [i for i in coords[0]] ys = [i for i in coords[1]] try: intensity_value = np.median(spatial_entity_to_values[name]) except: intensity_value = 0.0 entity_xs.append(xs) entity_ys.append(ys) entity_names.append(name) entity_rates.append(intensity_value) palette.reverse() color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=entity_xs, y=entity_ys, name=entity_names, rate=entity_rates, )) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="London Median House Prices, 2013-2014", tools=TOOLS, x_axis_location=None, y_axis_location=None) p.grid.grid_line_color = None p.patches('x', 'y', source=source, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ("Name", "@name"), ("Price change rate)", "@rate%"), ("(Long, Lat)", "($x, $y)"), ] show(p)
def hot_map_init(self, width=700, height=700, webgl=True): self.districts = NYCGeoPolygon.load_districts() rates = [] for district in self.districts: x, y = district.xy() self.districts_xs.append(x) self.districts_ys.append(y) self.districts_names.append(district.name) rates.append( self.data.pickups[district.index]) # default uses pickups self.hot_map_source = ColumnDataSource(data=dict( x=self.districts_xs, y=self.districts_ys, name=self.districts_names, rate=rates, )) palette.reverse() color_mapper = LogColorMapper(palette=palette) self.hot_map = figure(webgl=webgl, plot_height=height, plot_width=width, tools='pan,wheel_zoom,box_zoom,reset,hover,save', x_axis_location=None, y_axis_location=None) self.hot_map.grid.grid_line_color = None self.hot_map.patches('x', 'y', source=self.hot_map_source, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) self.hot_map.title.text = "%s %s/%s, %s" % \ (self.selected_type, self.selected_year, self.selected_month, NYCBorough.BOROUGHS[self.selected_borough]) hover = self.hot_map.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ("District", "@name"), ("Trips", "@rate"), ("Coordinates", "($x, $y)"), ]
def plot(location, predictions, state_name): from bokeh.sampledata.us_counties import data as counties palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] in [state_name] } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values() ] #Make sure names match with data county_rates = [unemployment[county_id] for county_id in counties] #These would be the predictions color_mapper = LogColorMapper(palette=palette) data = dict( x=county_xs, y=county_ys, name=county_names, rate=predictions, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Vote Preference Predictions", tools=TOOLS, #x_axis_location=None, y_axis_location=None,tooltips=[("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)")]) x_axis_location=None, y_axis_location=None, tooltips=[("Name", "@name"), ("Yes vote (percent)", "@rate%")]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) show(p) script, div = components(p) return script, div
def choropleth(df): palette.reverse() if "HI" in states: del states["HI"] del states["AK"] state_xs = [states[code]["lons"] for code in states] state_ys = [states[code]["lats"] for code in states] state_names = [name for name in states] state_rates = [df[state] if state in df else 0 for state in states] color_mapper = LogColorMapper( palette="Viridis256", low=0, high=max(state_rates)) data = dict( x=state_xs, y=state_ys, name=state_names, rate=state_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( width=850, height=500, title="Location of Charities in Forward Investivation", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("State", "@name"), ("Number of Charities", "@rate") ]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={'field': 'rate', 'transform': color_mapper}, fill_alpha=0.7, line_color="black", line_width=0.5) color_bar = ColorBar( color_mapper=color_mapper, ticker=FixedTicker( ticks=[1, 10, 20, 50, 100, 500]), label_standoff=8, border_line_color=None, location=(0, 0)) p.add_layout(color_bar, 'right') show(p)
def gen_texas(): from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)") ]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={'field': 'rate', 'transform': color_mapper}, fill_alpha=0.7, line_color="white", line_width=0.5) show(p)
class texas: #https://bokeh.pydata.org/en/latest/docs/gallery/texas.html palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)") ]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={'field': 'rate', 'transform': color_mapper}, fill_alpha=0.7, line_color="white", line_width=0.5) show(p)
from bokeh.io import show from bokeh.models import (ColumnDataSource, HoverTool, LogColorMapper) from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment if __name__ == "__main__": palette.reverse() counties = {code: county \ for code, county in counties.items() \ if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates)) p = figure(title="Texas Unemployment, 2009", tools="pan,wheel_zoom,reset,hover,save", x_axis_location=None, y_axis_location=None) p.grid.grid_line_color = None
from bokeh.io import show from bokeh.models import ( ColumnDataSource, HoverTool, LogColorMapper ) from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ))
# In[2]: from bokeh.io import show # importing show function to display the output from bokeh.models import ( ColumnDataSource, HoverTool, # hovertool helps the user to hover over the map and give details LogColorMapper # fills the color ) from bokeh.palettes import Viridis6 as palette # imports different palettes from bokeh.plotting import figure # import different figures from bokeh.sampledata.us_counties import data as counties # importing data of counties from bokeh.sampledata.unemployment import data as unemployment # importing data of unemployment within counties palette.reverse() # reverses colors of the palette counties = { code: county for code, county in counties.items() if county["state"] == "nj" } # specify the coude for county state Nj county_xs = [county["lons"] for county in counties.values()] # plotting x axis county_ys = [county["lats"] for county in counties.values()] # plotting y axis county_names = [county['name'] for county in counties.values()] # assigning names of county data county_rates = [unemployment[county_id] for county_id in counties] # assigning unemployment rate of county color_mapper = LogColorMapper(palette=palette) # using the color mapper funtion to fill the map according to the rates source = ColumnDataSource(data=dict( x=county_xs, # assigining x axis y=county_ys, # assigining y axis
def map(): from bokeh.sampledata.us_counties import data as counties from bokeh.sampledata.unemployment import data as unemployment palette.reverse() counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) source = ColumnDataSource(data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, )) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure(title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None) p.grid.grid_line_color = None p.patches('x', 'y', source=source, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5) hover = p.select_one(HoverTool) hover.point_policy = "follow_mouse" hover.tooltips = [ ("Name", "@name"), ("Unemployment rate)", "@rate%"), ("(Long, Lat)", "($x, $y)"), ] #show(p) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(p) html = render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)