Adding hovertool to a second Y axis with bokeh python

Mueladavc

This is a example of what I have so far, just for ilustration.

import numpy as np
from bokeh.models import ColumnDataSource, Range1d, LinearAxis, HoverTool
from bokeh.plotting import figure, output_file, show
import pandas as pd
import datetime

base_df = pd.DataFrame()
ini = datetime.datetime.today().date()
base_df['Date'] = [ini - datetime.timedelta(days=x) for x in range(0, 60)]
base_df['Y_axis_1'] = [np.random.random()*100 for x in range(0, 60)]
base_df['Y_axis_2'] = [np.random.random()*100 + 100 for x in range(0, 60)]
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
xdr = Range1d(start=0, end=100)
yplt = Range1d(start=100, end=0)

source = ColumnDataSource(base_df)

plot = figure(tools=TOOLS, toolbar_location="above", logo=None, y_range=yplt, plot_width=1200, plot_height=700, title='Multi_Yaxis_hover', x_axis_type='datetime')
plot.background_fill_color = "#dddddd"
plot.xaxis.axis_label = "Date"
plot.yaxis.axis_label = "Range_1"
plot.grid.grid_line_color = "white"

plot.triangle('Date', 'Y_axis_1', size=6, source=source, color='green', line_color="green", fill_alpha=0.8, legend='Axis_Y_1')

plot.extra_y_ranges = {"foo": Range1d(start=100, end=200)}
plot.add_layout(LinearAxis(y_range_name="foo", axis_label="Range_2",), 'right')

plot.circle(base_df.Date, base_df.Y_axis_2, y_range_name='foo', color='brown', legend='Axis_Y_2')
hover1 = HoverTool(tooltips=[("Value", "@Y_axis_1"), ("Date", "@Date{%F}")],
                   formatters={'Date': 'datetime'}, mode='mouse', line_policy='nearest')
plot.add_tools(hover1)

output_file('Multi_Yaxis_hover.html', title='Multi_Yaxis_hover example')

show(plot)

I tried to set a second hovertool to the extra Y axis just like in the code below:

hover2 = HoverTool(tooltips=[("Value", "@Y_axis_2"), ("Date", "@Date{%F}")],
                   formatters={'Date': 'datetime'}, mode='mouse', line_policy='nearest')
plot.add_tools(hover2)

But it doesn't have effect over the second axis, only over the first. How Do I set hovertool to the second Y axis? Thanks

bigreddot

When you use a tooltip value with an @ in front, e.g. @Y_axis_2, that always and only ever refers to values in a data source column. Nothing else. So unless you have a column "Y_axis_2" in your data source, it is completely expected that nothing would show up.

It sounds like you want the coordinates directly under the mouse to be displayed? The documentation describes the few distinguished "special variables" for things like that. They all start with $. The $x and $y will display the coordinates under the mouse. However, as of Bokeh 0.13, it can only do so for the primary (first) axis. Displaying values from a different axis would require new feature development work, so a GitHub feature request issue would be appropriate.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related