Search code examples
pythonbokehholoviewshvplot

How to autohide the Bokeh toolbar when using holoviews


The holoviews documentation mentions that the Bokeh toolbar can be hidden or moved: http://holoviews.org/user_guide/Plotting_with_Bokeh.html The bokeh document shows how to autohide the toolbar so it only shows on mouse-over.

Is it possible to autohide the toolbar when using holoviews as it doesn't allow me to pass options like toolbar='autohide'

Any help would be most welcome.

fundYear.hvplot.bar(
    x='year',
    y='fundingReq',
    rot=90,
).opts(
    toolbar='left',
    title="Funding Requested per Year",
    yformatter='$%f',
)

Solution

  • Possible settings for the position of the toolbar are:

    ['above', 'below', 'left', 'right', 'disable', None]

    So you can't set autohide like that, but...

    1) You can use hooks to set autohide.

    With hooks you can customize the plot just before it will be plotted.

    def set_toolbar_autohide(plot, element):
        bokeh_plot = plot.state
        bokeh_plot.toolbar.autohide = True
    
    your_plot.opts(hooks=[set_toolbar_autohide], backend='bokeh')
    

    You can also find useful info on hooks in the FAQ:
    https://holoviews.org/FAQ.html

    2) Another solution would be to convert your Holoviews plot to an actual bokeh plot and then set the bokeh toolbar to autohide:

    Quick solution is basically:

    my_bokeh_plot = hv.render(my_hv_plot, backend='bokeh')
    my_bokeh_plot.toolbar.autohide = True
    

    Full working example of 2nd solution:

    # import libraries
    import numpy as np
    import pandas as pd
    
    import holoviews as hv
    import hvplot.pandas
    hv.extension('bokeh', logo=False)
    
    from bokeh.plotting import show
    
    
    # create sample dataframe
    df = pd.DataFrame({
        'col1': np.random.normal(size=30),
        'col2': np.random.normal(size=30),
    })
    
    # create holoviews plot
    my_hv_plot = df.hvplot.scatter(label='Scattering around', width=700)
    
    # turn plot into bokeh plot
    my_bokeh_plot = hv.render(my_hv_plot, backend='bokeh')
    
    # set toolbar to autohide
    my_bokeh_plot.toolbar.autohide = True
    
    # show plot
    show(my_bokeh_plot)