这是我到目前为止的例子,仅供参考。
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)
我试图将第二个悬停工具设置到额外的Y轴上,就像下面的代码一样:
hover2 = HoverTool(tooltips=[("Value", "@Y_axis_2"), ("Date", "@Date{%F}")],
formatters={'Date': 'datetime'}, mode='mouse', line_policy='nearest')
plot.add_tools(hover2)
但是它在第二个轴上没有作用,仅在第一个轴上有作用。 如何将悬停工具设置为第二个Y轴? 谢谢
答案 0 :(得分:2)
如果您使用的工具提示值前面带有@
,例如@Y_axis_2
,它始终且仅引用数据源列中的值。没有其他的。因此,除非您在数据源中有一列"Y_axis_2"
,否则完全不会出现任何内容。
听起来您想直接在鼠标下方显示坐标?该文档描述了少数类似的"special variables"。它们都以$
开头。 $x
和$y
将在鼠标下方显示坐标。但是,从散景0.13开始,它只能在主轴(第一)轴上使用。从不同的轴显示值将需要进行新的功能开发工作,因此GitHub功能请求问题将是适当的。
答案 1 :(得分:0)
TOOLS = "pan,box_zoom,reset,lasso_select,save,box_select,xzoom_in,crosshair"
# create a new plot (with a title) using figure
source_data = {'x':x, 'yy':yy, 'y':y}
source = ColumnDataSource(source_data)
p = figure(
plot_width=1500,
plot_height=500,
title="My Line Plot",
tools=TOOLS,
x_axis_type="datetime",
y_range=(0,y.max()+1)
)
line = p.line(x='x', y='y', line_color="orange", line_width=2, source=source)
p.extra_y_ranges = {"volume": Range1d(start=0, end=1.5*np.max(yy))}
p.vbar(x='x', top='yy', width=0.5,color="grey", y_range_name="volume",source=source)
p.add_layout(LinearAxis(y_range_name="volume"), 'right')
hover1 = HoverTool(
tooltips = [
("updatedAt", "@x{%Y-%m-%d %H:%M:%S}"),
("layPrice0", "@y{0.00}"),
("laySize0", "@yy{0}"),
],
formatters={
"x": "datetime",
"y": "numeral",
"yy": "numeral",
},
)
p.add_tools(hover1)
p.xaxis.formatter=DatetimeTickFormatter(
minutes = ["%Y-%m-%d %H:%M:%S"],
hourmin = ["%Y-%m-%d %H:%M:%S"],
hours=["%Y-%m-%d %H:%M:%S"],
days=["%Y-%m-%d %H:%M:%S"],
months=["%Y-%m-%d %H:%M:%S"],
years=["%Y-%m-%d %H:%M:%S"],
)
output_file("legend.html", title="legend.py example")
show(p) # open a browser