Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link the CrossHairTool in bokeh over several plots?

Tags:

python

bokeh

When moving the crosshair (dimensions=width) in one plot I want to see the same position in the other plot(s). My plots share the same x-axis.

Here is the plot setup and an example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bokeh.plotting import figure, ColumnDataSource, output_file, save
from bokeh.models import Span, CrosshairTool, HoverTool, ResetTool, PanTool, WheelZoomTool
from datetime import datetime



def timeline_figure(title=None, x_range=None, y_range=None):

    # TODO: align x-axis

    # TOOLS = "resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,save"
    # TOOLS = "resize,crosshair,xpan,xwheel_zoom,box_zoom,reset,save"
    TOOLS = [CrosshairTool(dimensions=['height']),
             PanTool(dimensions=['width']),
             HoverTool(tooltips=[("Dato", "@Date")]),
             WheelZoomTool(dimensions=['width']),
             ResetTool()]

    # Setting up the bokeh figure
    fig = figure(width=800, height=250, title=title, x_axis_type="datetime",
                 x_range=x_range, y_range=y_range, tools=TOOLS)

    # make the outline "invisible"
    fig.outline_line_color = 'white'

    # change just some things about the x-grid
    fig.xgrid.grid_line_color = None
    fig.ygrid.grid_line_color = None
    # change just some things about the y-grid

    fig.yaxis.minor_tick_line_color = None

    year = 2016
    dec = Span(location=datetime(year-1, 12, 1, 0, 0, 0).timestamp() * 1000,
                    dimension='height', line_color='grey', line_dash='dashed', line_width=1)
    jan = Span(location=datetime(year, 1, 1, 0, 0, 0).timestamp() * 1000,
               dimension='height', line_color='grey', line_dash='dashed', line_width=1)
    feb = Span(location=datetime(year, 2, 1, 0, 0, 0).timestamp() * 1000,
               dimension='height', line_color='grey', line_dash='dashed', line_width=1)
    mar = Span(location=datetime(year, 3, 1, 0, 0, 0).timestamp() * 1000,
               dimension='height', line_color='grey', line_dash='dashed', line_width=1)
    apr = Span(location=datetime(year, 4, 1, 0, 0, 0).timestamp() * 1000,
               dimension='height', line_color='grey', line_dash='dashed', line_width=1)
    may = Span(location=datetime(year, 5, 1, 0, 0, 0).timestamp() * 1000,
               dimension='height', line_color='grey', line_dash='dashed', line_width=1)

    fig.renderers.extend([dec, jan, feb, mar, apr, may])

    return fig


def usage():
    import numpy as np
    from datetime import timedelta
    from bokeh.io import gridplot

    output_file("test.html", mode="cdn")

    d_start = datetime(2016, 6, 1)
    d_step = timedelta(days=1)

    t = [d_start + (i * d_step) for i in range(0, 12)]
    s1 = np.random. randint(2, 10, 12)
    s2 = np.random.randint(2, 10, 12)
    source = ColumnDataSource({'t': t, 's1': s1, 's2': s2})

    p1 = timeline_figure()
    p1.triangle(x='t', y='s1', source=source, size=10, color="blue")
    p2 = timeline_figure()
    p2.square(x='t', y='s2', source=source, size=10, color="red")

    p = gridplot([[p1], [p2]])
    save(p)

if __name__ == "__main__":
    usage()

Grateful for any advice.

Karsten

like image 226
karsten Avatar asked Jun 22 '16 10:06

karsten


People also ask

How do you zoom in on bokeh?

Zoom In. Bokeh can be achieved at any focal length, but if you're struggling to get a strong bokeh effect, try zooming in more, or using a lens with a longer focal length. If zooming in means you can't fit your subject in the frame, move further away from your subject and re-shoot.

What is hover tool?

The hover tool displays tooltips associated with individual glyphs. You can configure these tooltips to activate in different ways with a mode property: "mouse" only when the mouse is directly over a glyph.


1 Answers

As of bokeh v2.2.1 the solution is simplified. I am not sure whether it was already possible like this in previous versions. Here an example for sharing crosshair for both dimensions between 9 plots in a gridplot in bokeh v2.2.1:

import numpy as np
from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import CrosshairTool

plots = [figure() for i in range(6)]
[plot.line(np.arange(10), np.random.random(10)) for plot in plots]
crosshair = CrosshairTool(dimensions="both")
for plot in plots:
    plot.add_tools(crosshair)

show(gridplot(children=[plot for plot in plots], ncols=3))
like image 200
w-mv Avatar answered Sep 28 '22 03:09

w-mv