pythonbokehholoviews

Predefine selection of Holoviews RangeToolLink


Is there a way to adjust the selection range in the RangeToolLink?

By default, the entire area of the plot is selected, which can then be adjusted and/or moved accordingly.

Standard: enter image description here

Wish (for example): enter image description here

(From https://www.holoviews.org/gallery/demos/bokeh/timeseries_range_tool.html)


Solution

  • In Holoviews it is possible to set the underling properties of the figure using a hook.

    In the example below there are two different hooks used for the upper and lower figure.

    import pandas as pd
    import holoviews as hv
    from holoviews import opts
    from holoviews.plotting.links import RangeToolLink
    
    hv.extension('bokeh')
    from bokeh.sampledata.stocks import AAPL
    
    aapl_df = pd.DataFrame(AAPL['close'], columns=['close'], index=pd.to_datetime(AAPL['date']))
    aapl_df.index.name = 'Date'
    
    aapl_curve = hv.Curve(aapl_df, 'Date', ('close', 'Price ($)'))
    
    def hook(plot, element):
        plot.handles['x_range'].start = pd.to_datetime('2006')
        plot.handles['x_range'].end = pd.to_datetime('2008')
    tgt = aapl_curve.relabel('AAPL close price').opts(width=800, labelled=['y'], toolbar='disable').opts(hooks=[hook])
    
    def hook(plot, element):
        plot.handles['x_range'].start = pd.to_datetime('2000')
        plot.handles['x_range'].end = pd.to_datetime('2014')
    src = aapl_curve.opts(width=800, height=100, yaxis=None, default_tools=[]).opts(hooks=[hook])
    
    RangeToolLink(src, tgt)
    
    layout = (tgt + src).cols(1)
    layout.opts(opts.Layout(shared_axes=False, merge_tools=False))
    

    result after modification of the range tool

    The result is very similar to the range_tool example of the bokeh library.