def _get_provincial_map( plot_width, source, tibet_source, legend_data, fill_color, line_color='black', line_width=0.5, tooltip_text='' ): p_map = get_map_plot(plot_width) provinces = Patches( xs='xs', ys='ys', fill_color=fill_color, line_color=line_color, line_width=line_width, line_join='round', ) pr = p_map.add_glyph(source, provinces) tibet = Patches( xs='xs', ys='ys', fill_color='white', line_color='gray', line_dash='dashed', line_width=0.5, ) tr = p_map.add_glyph(tibet_source, tibet) # Add legend legend_source = ColumnDataSource(legend_data) rect = Rect( x='x', y=map_legend_y, height=1, width=0.3, fill_color='color', line_color=None, ) p_map.add_glyph(legend_source, rect) # Add start val text_start = [legend_data.vals[0][:-2]] p_map.add_glyph( ColumnDataSource( dict(x=[map_legend_x], y=[map_legend_y - 3.5], text=text_start) ), Text(x='x', y='y', text='text', text_font_size='7pt', text_align='left') ) # Add end val text_end = [legend_data.vals[99][:-2]] # Note the 99 is dependent on legend having 100 points if len(text_end[0]) > 5: text_end = [legend_data.vals[99][0:5]] p_map.add_glyph( ColumnDataSource( dict(x=[map_legend_x + 25], y=[map_legend_y - 3.5], text=text_end) ), Text(x='x', y='y', text='text', text_font_size='8pt', text_align='right') ) # Add hovers tooltips = "<span class='tooltip-text'>@name_en</span>" tooltips = tooltips + "<span class='tooltip-text'>%s</span>" % tooltip_text p_map.add_tools(HoverTool(tooltips=tooltips, renderers=[pr])) tibet_tooltips = "<span class='tooltip-text'>@name_en (No data)</span>" p_map.add_tools(HoverTool(tooltips=tibet_tooltips, renderers=[tr])) return p_map
def mapa(dfsource, indice_label, indice_name, color_mapper, plot_width, plot_height): mapa_vulnerabilidade = figure( tools="pan,box_zoom,wheel_zoom,undo,reset,hover,tap", plot_width=plot_width, plot_height=plot_height, ) my_tile = bokeh.models.tiles.WMTSTileSource( url= 'https://api.mapbox.com/styles/v1/rdferrari/cj78ytz6l51pw2so2o3bj2d09/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoicmRmZXJyYXJpIiwiYSI6ImNpcDQ4Nm12YjAwNnJsZ20zOXdmc29obmcifQ.Shb8a3P1XAV8cPWR1yGDTA' ) # tile=mapa_vulnerabilidade.add_tile(my_tile) mapa_vulnerabilidade.grid.grid_line_color = None mapa_vulnerabilidade.axis[0].visible = False mapa_vulnerabilidade.axis[1].visible = False mapa_renderer = mapa_vulnerabilidade.patches( 'x', 'y', source=dfsource, fill_color={ 'field': 'indice', 'transform': color_mapper }, fill_alpha=1.0, line_color="white", line_width=0.5, ) mapa_renderer.nonselection_glyph = Patches( fill_color={ 'field': 'indice', 'transform': color_mapper }, fill_alpha=1.0, line_color="white", line_width=0.5, ) mapa_renderer.selection_glyph = Patches( fill_color={ 'field': 'indice', 'transform': color_mapper }, fill_alpha=1.0, line_color="gray", line_width=5.0, line_alpha=0.8, ) hover_mapa = mapa_vulnerabilidade.select_one(HoverTool) hover_mapa.point_policy = "follow_mouse" hover_mapa.tooltips = [ ("Município", "@municipio"), ('Índice', '@indice{0.00}'), ('Meta Financeira', 'R$ @meta{0,0.00}'), ('Realizado', 'R$ @realizado{0,0.00}'), ] mapa_vulnerabilidade.toolbar.logo = None mapa_vulnerabilidade.axis[0].visible = False mapa_vulnerabilidade.toolbar_location = 'above' return mapa_vulnerabilidade
def altitude_profile(data): plot = Plot(plot_width=800, plot_height=400) plot.title.text = "%s - Altitude Profile" % name plot.y_range.range_padding = 0 xaxis = LinearAxis(axis_label="Distance (km)") plot.add_layout(xaxis, 'below') yaxis = LinearAxis(axis_label="Altitude (m)") plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) # x grid plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) # y grid plot.add_tools(PanTool(), WheelZoomTool(), ResetTool()) X, Y = data.dist, data.alt y0 = min(Y) patches_source = ColumnDataSource( dict(xs=[[X[i], X[i + 1], X[i + 1], X[i]] for i in range(len(X[:-1]))], ys=[[y0, y0, Y[i + 1], Y[i]] for i in range(len(Y[:-1]))], color=data.colors[:-1])) patches = Patches(xs="xs", ys="ys", fill_color="color", line_color="color") plot.add_glyph(patches_source, patches) line_source = ColumnDataSource(dict(x=data.dist, y=data.alt)) line = Line(x='x', y='y', line_color="black", line_width=1) plot.add_glyph(line_source, line) return plot
def construct_map(source, fill_string='water_color', selected_color=ORANGE): assert isinstance(source, ColumnDataSource), "Require ColumnDataSource" # Plot and axes x_start, x_end = (-18, 55) y_start, y_end = (-35, 38) xdr = Range1d(x_start, x_end) ydr = Range1d(y_start, y_end) aspect_ratio = (x_end - x_start) / (y_end - y_start) plot_height = 600 plot_width = int(plot_height * aspect_ratio) plot = Plot(x_range=xdr, y_range=ydr, title="", plot_width=plot_width, plot_height=plot_height, min_border=0, **PLOT_FORMATS) borders = Patches( xs='xs', ys='ys', fill_color=fill_string, fill_alpha=1, line_color="#FFFFFF", line_width=1, ) selected_borders = Patches( xs='xs', ys='ys', fill_color=fill_string, fill_alpha=1, line_color=selected_color, line_width=3, ) plot.add_glyph(source, borders, selection_glyph=selected_borders, nonselection_glyph=borders) # nopep8 return plot
def mapa(source,color_mapper,plot_width,plot_height): mapa_fig = figure( tools="hover", plot_width=plot_width, plot_height=plot_height, ) mapa_fig.grid.grid_line_color = None mapa_fig.axis[0].visible=False mapa_fig.axis[1].visible=False mapa_renderer=mapa_fig.patches( 'x', 'y', source=source, fill_color={'field': 'indicador_selecionado', 'transform': color_mapper}, fill_alpha=1.0, line_color="gray", line_width=0.5, ) mapa_renderer.nonselection_glyph=Patches( fill_color={'field': 'indicador_selecionado', 'transform': color_mapper}, fill_alpha=1.0, line_color="gray", line_width=0.5, ) mapa_renderer.selection_glyph=Patches( fill_color={'field': 'indicador_selecionado', 'transform': color_mapper}, fill_alpha=1.0, line_color="gray", line_width=2.0, line_alpha=0.8, ) hover_mapa = mapa_fig.select_one(HoverTool) hover_mapa.point_policy = "follow_mouse" hover_mapa.tooltips = [ ("Centro Rede CEDES:", "@nome_universidade"), ('Indicador','@indicador_selecionado{0.00}'), ] mapa_fig.toolbar.logo=None mapa_fig.axis[0].visible=False mapa_fig.toolbar_location=None return mapa_fig
def plot_map(df, fname): """ Generate the heat map """ map_options = GMapOptions(lat=40.75, lng=-73.9, map_type='roadmap', zoom=11) API_KEY = 'AIzaSyC-4SnwvK3u2CR-zh-4zl7J_msCmDfq_Sg' palette = PuRd[7] palette.reverse() color_mapper = LinearColorMapper(palette=palette) plot = GMapPlot( x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, api_key=API_KEY, plot_width=1000, plot_height=1000, ) source = ColumnDataSource(data=dict( lat=df['lat'].tolist(), lng=df['lng'].tolist(), grp=df['district'], dur=df['duration'], durt=df['durt'], ct=df['ct'], )) patches = Patches(xs='lng', ys='lat', fill_color={ 'field': 'dur', 'transform': color_mapper }, fill_alpha=0.7, line_color="blue", line_width=1.0) plot.add_glyph(source, patches, name='patches') hover = HoverTool(names=['patches'], tooltips=[("Neighborhood", "@grp"), ("Avg Trip Duration", "@durt"), ("Trip Count", "@ct")]) plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool(), hover) output_file(fname) return plot
def populate_glyphs(self): self.plot.renderers = [] self.plot.tools = [] if self._show_counties: print "showing you the counties" #datasource = ColumnDataSource(county_data) #apatch = Patches(xs=county_xs, ys=county_ys, fill_color='white') #apatch = Patches(xs='xs', ys='ys', fill_color='colors', fill_alpha="alpha") apatch = Patches(xs='xs', ys='ys', fill_color='thecolors', fill_alpha='alpha') self.plot.add_glyph(self.county_source, apatch, name='counties') if self._show_hotels: print "showing you the hotels" circle2 = Circle(x="lon", y="lat", size=10, fill_color="fill2", fill_alpha=1.0, line_color="black") circle = Circle(x="lon", y="lat", size=10, fill_color="fill", fill_alpha=1.0, line_color="black") #print "source is ", self.source['lon'], self.source['lat'], self.source['f1ll'] self.plot.add_glyph(self.source, circle2, nonselection_glyph=circle, name='hotels') #county_xs, county_ys = get_some_counties() rndr = self.plot.renderers[-1] pan = PanTool() wheel_zoom = WheelZoomTool() box_select = BoxSelectTool() box_select.renderers = [rndr] tooltips = "@name" tooltips = "<span class='tooltip-text'>@names</span>\n<br>" tooltips += "<span class='tooltip-text'>Reviews: @num_reviews</span>" hover = HoverTool(tooltips=tooltips, names=['hotels']) tap = TapTool(names=['hotels']) self.plot.add_tools(pan, wheel_zoom, box_select, hover, tap) overlay = BoxSelectionOverlay(tool=box_select) self.plot.add_layout(overlay)
def test_patches_hover_still_works_when_a_seleciton_is_preselcted( output_file_url, selenium): # This tests an edge case interaction when Patches (specifically) is used # with a tool that requires hit testing e.g. HitTool AND a selection is # pre-made on the data source driving it. plot = Plot(x_range=Range1d(0, 100), y_range=Range1d(0, 100), min_border=0) source = ColumnDataSource( dict(xs=[[0, 50, 50, 0], [50, 100, 100, 50]], ys=[[0, 0, 100, 100], [0, 0, 100, 100]], color=['pink', 'blue'])) source.selected = { '0d': { 'glyph': None, 'indices': [] }, '1d': { 'indices': [1] }, '2d': { 'indices': {} }, } plot.add_glyph(source, Patches(xs='xs', ys='ys', fill_color='color')) plot.add_tools(HoverTool()) plot.add_layout(LinearAxis(), 'below') plot.add_layout(LinearAxis(), 'left') save(plot) selenium.get(output_file_url) assert has_no_console_errors(selenium) # Hover plot and test no error canvas = selenium.find_element_by_tag_name('canvas') actions = ActionChains(selenium) actions.move_to_element_with_offset(canvas, 100, 100) actions.perform() # If this assertion fails then there were likely errors on hover assert has_no_console_errors(selenium)
rate=[ unemployment[code] for code in us_counties if us_counties[code] ["state"] not in ["ak", "hi", "pr", "gu", "vi", "mp", "as"] ], )) plot = Plot(min_border=0, border_fill_color="white", plot_width=1300, plot_height=700) plot.title.text = "2009 Unemployment Data" plot.toolbar_location = None county_patches = Patches(xs="county_xs", ys="county_ys", fill_color=transform("rate", cmap), fill_alpha=0.7, line_color="white", line_width=0.5) plot.add_glyph(county_source, county_patches) state_patches = Patches(xs="state_xs", ys="state_ys", fill_alpha=0.0, line_color="#884444", line_width=2) plot.add_glyph(state_source, state_patches) cbar = ColorBar(color_mapper=cmap, location=(0, 0)) plot.add_layout(cbar, 'left') doc = Document()
def make_patches_plot(x_range, y_range, source): with make_plot(x_range, y_range, source) as plot: glyph = Patches(xs='xs', ys='ys', fill_color='value', fill_alpha=0.8, line_color='#CCCCCC', line_alpha=0.8) plot.add_glyph(source, glyph) return plot
plot_height=300, min_border=0, title="Hover and tap discontinuous patches.") patches_dis_data = pd.DataFrame( { 'xs': [[0, 49, 49, 0, NAN, 51, 89, 89, 51], [90, 100, 100, 90], [92, 98, 98, 92]], 'ys': [[0, 0, 80, 80, NAN, 0, 0, 90, 90], [0, 0, 100, 100], [20, 20, 90, 90]], 'value': ['I am one object split in two', 'I am wheat', 'I am steelblue'], 'fill_color': ['firebrick', 'wheat', 'steelblue'] }, ) plot_patches_dis = Plot(**PLOT_FORMATS) plot_patches_dis.add_glyph( ColumnDataSource(patches_dis_data), Patches( xs='xs', ys='ys', fill_color='fill_color', line_color=value('wheat'), )) plot_patches_dis.add_tools(HoverTool(tooltips='@value')) plot_patches_dis.add_tools(TapTool()) filename = '2376_hover_on_discontinuous_patches.html' output_file(filename, title='Discontinuous Patches', mode='relative-dev') save(plot_patches_dis) view(filename)
print("HERE?") # Have to add the patches after the tiles input_areas = p.patches('xs', 'ys', source=geosource, fill_color='red', line_color='gray', line_width=0.25, fill_alpha=.3) taptool = TapTool(renderers=[input_areas]) hovertool = HoverTool(renderers=[input_areas], tooltips=[('Site', '@name')]) selected_patches = Patches(fill_color="yellow", fill_alpha=.8, line_width=1, line_color="green") input_areas.selection_glyph = selected_patches geosource.selected.on_change('indices', tapselect_handler) print("HERE?") # CustomJS(code=""" # // comment.... # console.log("Somewhere to stop!"); # """) # Create hover tool p.add_tools(taptool, hovertool)
xpts = np.array([-.09, -.12, .0, .12, .09]) ypts = np.array([-.1, .02, .1, .02, -.1]) source = ColumnDataSource( dict( xs=[xpts * (1 + i / 10.0) + xx for i, xx in enumerate(x)], ys=[ypts * (1 + i / 10.0) + yy for i, yy in enumerate(y)], )) plot = Plot(title=None, plot_width=300, plot_height=300, min_border=0, toolbar_location=None) glyph = Patches(xs="xs", ys="ys", fill_color="#fb9a99") plot.add_glyph(source, glyph) xaxis = LinearAxis() plot.add_layout(xaxis, 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) curdoc().add_root(plot) show(plot)
def plot_data(data_df, connections, year, geoSource_new): data_df_countries = data_df #.drop_duplicates(subset=None, keep='first', inplace=True) connections_df = connections node_source = ColumnDataSource( data_df_countries[["country_id", "Country", "Longitude", "Latitude"]]) edge_source = ColumnDataSource(connections_df[["start", "end"]]) node_renderer = GlyphRenderer(data_source=node_source, glyph=node_glyph, selection_glyph=node_selection, nonselection_glyph=node_nonselection) ## Create edge_renderer edge_renderer = GlyphRenderer(data_source=edge_source, glyph=edge_glyph, hover_glyph=edge_hover, selection_glyph=edge_selection, nonselection_glyph=edge_nonselection) ## Create layout_provider graph_layout = dict( zip(data_df_countries.country_id.astype(str), zip(data_df_countries.Longitude, data_df_countries.Latitude))) layout_provider = StaticLayoutProvider(graph_layout=graph_layout) ## Create graph renderer graph = GraphRenderer(edge_renderer=edge_renderer, node_renderer=node_renderer, layout_provider=layout_provider, inspection_policy=NodesAndLinkedEdges(), selection_policy=NodesAndLinkedEdges()) plot = Plot(x_range=Range1d(-150, 150), y_range=Range1d(15, 75), plot_width=800, plot_height=600, background_fill_color=Set3_12[4], background_fill_alpha=0.2) plot.title.text = "Human Trafficing Visualization for " + str(year) # plot.add_glyph( geoSource_data, Patches(xs='xs', ys='ys', line_color='grey' # , line_width=.5, fill_color=Set3_12[6], fill_alpha=0.25)) plot.add_glyph( geoSource_new, Patches(xs='xs', ys='ys', line_color='grey', line_width=.2, fill_color={ 'field': 'Tier', 'transform': mapper2 }, fill_alpha=0.25)) plot.renderers.append(graph) plot.add_layout(LinearAxis(axis_label="Latitude"), "below") plot.add_layout(LinearAxis(axis_label="Longitude"), "left") hover = HoverTool( show_arrow=True, # tooltips= # [("Country Involved: ", "@Country")], tooltips=""" <div> <div> <span style="font-size: 15px;">Country Information </span> <span style="font-size: 12px; color: #696;">@Destination_Country </span> </div> </div> """, renderers=[graph]) hover_no_tooltips = HoverTool(tooltips=None, renderers=[graph]) box_zoom = BoxZoomTool() plot.add_tools(hover, hover_no_tooltips, box_zoom, TapTool(), BoxSelectTool(), ResetTool(), WheelZoomTool()) plot.toolbar.active_inspect = [hover, hover_no_tooltips] plot.toolbar.active_drag = box_zoom plot.outline_line_color = "navy" plot.outline_line_alpha = 0.3 plot.outline_line_width = 3 plot.add_tile(STAMEN_TONER_LABELS) return plot
def plot_data(data_df, connections, year, geoSource_new, df): d_yr = df[df['Years'] == year] data_df_countries = d_yr.merge(data_df, left_on='Origin_Country', right_on='Country') #data_df_countries = node_dt #.drop_duplicates(subset=None, keep='first', inplace=True) connections_df = connections # node_source = ColumnDataSource(data_df_countries[["country_id","Country", "Longitude", "Latitude"]]) node_source = ColumnDataSource(data_df_countries) edge_source = ColumnDataSource(connections_df[["start", "end"]]) node_renderer = GlyphRenderer(data_source=node_source, glyph=node_glyph, selection_glyph=node_selection, nonselection_glyph=node_nonselection) ## Create edge_renderer edge_renderer = GlyphRenderer(data_source=edge_source, glyph=edge_glyph, hover_glyph=edge_hover, selection_glyph=edge_selection, nonselection_glyph=edge_nonselection) ## Create layout_provider graph_layout = dict( zip(data_df_countries.country_id.astype(str), zip(data_df_countries.Longitude, data_df_countries.Latitude))) layout_provider = StaticLayoutProvider(graph_layout=graph_layout) ## Create graph renderer graph = GraphRenderer(edge_renderer=edge_renderer, node_renderer=node_renderer, layout_provider=layout_provider, inspection_policy=NodesAndLinkedEdges(), selection_policy=NodesAndLinkedEdges()) plot = Plot(x_range=Range1d(-150, 150), y_range=Range1d(15, 75), plot_width=800, plot_height=600, background_fill_color=Set3_12[4], background_fill_alpha=0.2) plot.title.text = "Human Trafficing Visualization for " + str(year) plot.add_glyph( geoSource_new, Patches(xs='xs', ys='ys', line_color='grey', line_width=.2, fill_color={ 'field': 'Tier', 'transform': mapper2 }, fill_alpha=0.40)) plot.renderers.append(graph) plot.add_layout(LinearAxis(axis_label="Latitude"), "below") plot.add_layout(LinearAxis(axis_label="Longitude"), "left") # tooltips=""" # <div> # <div> # <span style="font-size: 17px; font-weight: bold;">@desc</span> # <span style="font-size: 15px; color: #966;">[$index]</span> # </div> # <div> # <span style="font-size: 15px;">Country Involved</span> # <span style="font-size: 10px; color: #696;">@Country</span> # </div> # </div> # """, hover = HoverTool( show_arrow=True, # tooltips= # [("Country Involved: ", "@Country")], tooltips=""" <div> <div> <span style="font-size: 13px;">Country Info</span> <span style="font-size: 12px; color: #696;">@Destination_Country, @Type_Of_Trafficking</span> </div> </div> """, renderers=[node_renderer], name='Test') hover_no_tooltips = HoverTool(tooltips=None, renderers=[graph]) box_zoom = BoxZoomTool() plot.add_tools(hover, hover_no_tooltips, box_zoom, TapTool(), BoxSelectTool(), ResetTool(), WheelZoomTool()) plot.toolbar.active_inspect = [hover, hover_no_tooltips] plot.toolbar.active_drag = box_zoom plot.outline_line_color = "navy" plot.outline_line_alpha = 0.3 plot.outline_line_width = 1 select_overlay = plot.select_one(BoxSelectTool).overlay select_overlay.fill_color = "firebrick" select_overlay.line_color = None zoom_overlay = plot.select_one(BoxZoomTool).overlay zoom_overlay.line_color = "olive" zoom_overlay.line_width = 3 zoom_overlay.line_dash = "solid" zoom_overlay.fill_color = None plot.add_tile(STAMEN_TONER_LABELS) return plot
y="y", w=0.4, h=0.4, url=dict(value="https://static.bokeh.org/logos/logo.png"), anchor="center")), ("line", Line(x="x", y="y", line_color="#F46D43")), ("multi_line", MultiLine(xs="xs", ys="ys", line_color="#8073AC", line_width=2)), ("multi_polygons", MultiPolygons(xs="xsss", ys="ysss", line_color="#8073AC", fill_color="#FB9A99", line_width=2)), ("patch", Patch(x="x", y="y", fill_color="#A6CEE3")), ("patches", Patches(xs="xs", ys="ys", fill_color="#FB9A99")), ("quad", Quad(left="x", right="xp01", top="y", bottom="ym01", fill_color="#B3DE69")), ("quadratic", Quadratic(x0="x", y0="y", x1="xp02", y1="y", cx="xp01", cy="yp01", line_color="#4DAF4A", line_width=3)), ("ray", Ray(x="x", y="y",
renderer = p.patches('x', 'y', source=source, fill_color={ 'field': 'veges', 'transform': color_mapper }, selection_line_color="blue", fill_alpha=0.7, line_color="white", line_width=0.5) selected_patches = Patches(fill_color={ 'field': 'veges', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=2) nonselected_patches = Patches(fill_color={ 'field': 'veges', 'transform': color_mapper }, fill_alpha=0.3, line_color="white", line_width=0.5) renderer.selection_glyph = selected_patches renderer.nonselection_glyph = nonselected_patches hover = p.select_one(HoverTool)