如何在Holoviews中实现Bokeh工具栏自动隐藏?
Absolutely! You can definitely get that auto-hiding toolbar working with Holoviews and Bokeh. Even though you can't pass a toolbar='autohide' parameter directly to Holoviews' opts(), we can tap into the underlying Bokeh objects to make this happen. Here's the straightforward solution:
方法1:使用Holoviews的Hook函数(推荐)
Holoviews provides hooks—small functions that let you modify the underlying Bokeh plot object during rendering. We can use this to enable the toolbar's autohide feature:
First, define a hook function that enables autohiding:
def enable_autohide_toolbar(plot, element): # Access the Bokeh toolbar and set autohide to True plot.toolbar.autohide = True
Then, add this hook to your plot's opts():
fundYear.hvplot.bar(x='year', y='fundingReq', rot=90)\ .opts( toolbar='left', title="Funding Requested per Year", yformatter='$%f', hooks=[enable_autohide_toolbar] # Add the hook here )
方法2:直接修改渲染后的Bokeh对象
If you prefer to handle things after rendering the Holoviews plot, you can convert it to a Bokeh object and tweak the toolbar directly:
# Create your Holoviews plot as usual hv_plot = fundYear.hvplot.bar(x='year', y='fundingReq', rot=90)\ .opts( toolbar='left', title="Funding Requested per Year", yformatter='$%f' ) # Convert to a Bokeh plot object bokeh_plot = hv.render(hv_plot) # Enable autohide on the toolbar bokeh_plot.toolbar.autohide = True # Now you can display bokeh_plot however you normally would
Either approach will make the toolbar automatically hide and only show up when you hover over the plot area—exactly what you're looking for. This works because Holoviews is built on top of Bokeh, so we can always reach down to the underlying Bokeh components when we need fine-grained control like this.
内容的提问来源于stack exchange,提问作者Brian Mc Donald

