Turn Bokeh Glyph into a link - python

I would like to turn all of the Bokeh glyphs on a certain plot into links to other pages. Is this possible?
For example if I had a map of countries, each country as a patch, if a user were to click on a country I would like to redirect them to that wikipedia page.

There also a simpler example in the User's Guide:
from bokeh.models import ColumnDataSource, OpenURL, TapTool
from bokeh.plotting import figure, output_file, show
output_file("openurl.html")
p = figure(plot_width=400, plot_height=400,
tools="tap", title="Click the Dots")
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
color=["navy", "orange", "olive", "firebrick", "gold"]
))
p.circle('x', 'y', color='color', size=20, source=source)
url = "http://www.colors.commutercreative.com/#color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)
show(p)

Related

How to add hover tool to the add_layout in python bokeh?

I want to add a hover tool to an Arrow in add_layout. However, it seems that the hover tool does not add anything to it. Here is my code:
from bokeh.models import Arrow, VeeHead, ColumnDataSource, HoverTool
from bokeh.plotting import figure, output_file, show
output_file("arrow.html", title="arrow.py example")
p = figure(plot_width=600, plot_height=600)
p.circle(x=[0, 1, 3], y=[0, 5, 0.7], radius=0.1,
color=["navy", "yellow", "red"], fill_alpha=0.1)
data = ColumnDataSource({"x_start": [0],
"x_end": [2],
"y_start": [0],
"y_end": [1]})
p.add_layout(Arrow(end=VeeHead(size=35), line_color="red", x_start="x_start", y_start="y_start", x_end="x_end", y_end="y_end", source = data, name="arrow"))
hover_tool = HoverTool(tooltips=[('x_start', '#x_start'), ("x_end", "#x_end"), ("y_start", "#y_start")], names=["arrow"])
p.add_tools(hover_tool)
show(p)
Arrows are annotations, not glyphs, and do not support hit-testing. It is not possible to attach a hover tool to an arrow.

Hide annotation alongside legend in Bokeh

I have an interactive Bokeh plot that's able to hide certain circle plots when clicking on the Legend. Now when I disable a plot by clicking on it, the plotted circles disappear but the annotations remain. Could someone explain to me how I can toggle those on/off all together or does anyone have a quick fix?
Here's a picture when it's toggled off:
I plot the circles + legend with the following code:
q.circle('lng', 'lat', source = source2, name='vri', color='red', size=5, hover_line_color="black", legend_label = 'VRI')
vri_labels = LabelSet(x='lng', y='lat', text='kruispuntn', x_offset=5, y_offset=5, source=source2, text_font_size = '10pt')
q.legend.location = "bottom_left"
q.legend.click_policy="hide"
q.add_layout(vri_labels)
show(q)
You can link the visible property via a CustomJS callback:
from bokeh.io import show
from bokeh.models import ColumnDataSource, LabelSet, CustomJS
from bokeh.plotting import figure
p = figure()
cds = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1], z=[1, 0]))
for var, params in [('y', {}),
('z', {'color': 'green'})]:
renderer = p.circle('x', var, source=cds, legend_label=var, size=20, **params)
label_set = LabelSet(x='x', y=var, text=var, source=cds, x_offset=5, y_offset=5)
p.add_layout(label_set)
renderer.js_on_change('visible', CustomJS(args=dict(ls=label_set),
code="ls.visible = cb_obj.visible;"))
p.legend.click_policy = 'hide'
show(p)

simple 1 layer area chart in bokeh

I am trying to create a simple area chart in bokeh (1 layer)
My attempt
df_example = pd.DataFrame(data= [['01-01-2018',10],['02-01-2018', 5 ], ['03-01-2018',7]], columns = ['date', 'value'] )
p = figure(plot_width=600, plot_height=400, x_range = df_example['date'])
p.Area(df_example, x='date', y='value')
show(p)
I get an error
AttributeError: 'Figure' object has no attribute 'Area'
Is the Area chart seems to be not available in bokeh anymore
Can anybody demonstrate how to get this type of chart please?
Bokeh recently added a varea_stack glyph method:
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y1=[1, 2, 4, 3, 4],
y2=[1, 4, 2, 2, 3],
))
p = figure(plot_width=400, plot_height=400)
p.varea_stack(['y1', 'y2'], x='x', color=("grey", "lightgrey"), source=source)
show(p)

Add hover to Bokeh without losing rest of tools

So with Bokeh I can do something like this to create a hover option:
From bokeh.models import HoverTool #add hover functionality
Hover = HoverTool(tooltips=[(name1:#column1), (name2:#columns2)])
Plot = figure(tools=[hover])
Plot.circle(x,y,hover_color=’red’)
However, by doing so, I lose the standard tools you get when you call figure() like pan, box_zoom, wheel_zoom, etc. I know I can add them back 1 by 1 inside the figure(tools=[]), but is there a way to only add hover to the rest of the default tools of figure(), after it is defined??
Thanks!
Use the add_tools() method, as outlined in the docs:
https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#specifying-tools
Slightly modified example from the docs:
from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool
output_file("toolbar.html")
# create a new plot with the toolbar below
p = figure(plot_width=400, plot_height=400,
title=None, toolbar_location="below")
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
p.add_tools(HoverTool())
show(p)

Python Bokeh: remove toolbar from chart

Note from maintainers: The specifics of this question concern the bokeh.charts API which is obsolete and was removed several years ago. In modern Bokeh, specify toolbar_location:
p = figure(toolbar_location=None)
OBSOLETE:
I don't seem to be able to remove the toolbar from a bokeh Bar chart. Despite setting the tools argument to None (or False or '') I always end up with the bokeh logo and a grey line, e.g. with this code:
from bokeh.charts import Bar, output_file, show
# prepare some data
data = {"y": [6, 7, 2, 4, 5], "z": [1, 5, 12, 4, 2]}
# output to static HTML file
output_file("bar.html")
# create a new line chat with a title and axis labels
p = Bar(data, cat=['C1', 'C2', 'C3', 'D1', 'D2'], title="Bar example",
xlabel='categories', ylabel='values', width=400, height=400,
tools=None)
# show the results
show(p)
However, when I try the same with a bokeh plot, it works perfectly fine and the toolbar is gone, e.g. with this code:
from bokeh.plotting import figure, output_file, show
output_file("line.html")
p = figure(plot_width=400, plot_height=400, toolbar_location=None)
# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
show(p)
Does anyone know what I'm doing wrong?
If you want to remove the logo and the toolbar you can do:
p.toolbar.logo = None
p.toolbar_location = None
Hope this resolves your problem
On any Bokeh plot object you can set:
p.toolbar_location = None

Categories

Resources