目标:获取Bokeh(python)提供的悬停/工具提示中的y轴值。
目前,我试过@y和@height都给我'???'悬停/工具提示中的值。我已经密切关注了Bokeh中悬停/工具提示周围的大部分documentation,但无济于事。
任何建议都会有所帮助......
以下是我正在使用的代码:
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df
from numpy import histogram, linspace
from bokeh.models import HoverTool
# Information contained within the hoover
hist, edges = histogram(data['Age'], density=False, bins=10)
hover = HoverTool( tooltips="""
<div style ="border-style: solid;border-width: 15px;border-color: gray;background-color:gray;padding:0">
<div>
<span style="font-size: 12px; color: white;font-family:century gothic;">'@y'</span>
<span style="font-size: 12px; color: white;font-family:century gothic;"> Observations</span>
</div>
</div style>
"""
)
p = figure(plot_height=300,tools=[hover])
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],line_color="white", line_width=1,color='gray',fill_alpha=.50)
p.title="Distribution of Age"
p.title_text_font = "Century Gothic"
p.title_text_font_style='normal'
p.title_location='above'
p.title_text_align= 'center'
p.xaxis.axis_label_text_font = "Century Gothic"
p.xaxis.axis_label_text_color = 'black'
p.xaxis.axis_label_text_font_style='normal'
p.yaxis.axis_label_text_font = "Century Gothic"
p.yaxis.axis_label_text_color = 'black'
p.yaxis.axis_label_text_font_style = "normal"
p.xaxis.axis_label_standoff = 10
p.yaxis.axis_label_standoff = 10
# Tufte style
p.background_fill_color = None
p.border_fill_color = None
p.min_border_left = 80
p.xaxis.major_tick_line_color = None # turn off x-axis major ticks
p.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks
p.yaxis.major_tick_line_color = None # turn off y-axis major ticks
p.yaxis.minor_tick_line_color = None # turn off y-axis minor ticks
p.ygrid.grid_line_color = "gray"
p.ygrid.grid_line_alpha = 0.25
p.ygrid.grid_line_width = 1
p.xgrid.grid_line_color = None
p.yaxis.axis_line_color = None
p.xaxis.axis_line_color = None
# Remove outline of graph
p.outline_line_color = None
# Remove Bokeh logo
#p.toolbar.logo = None
p.toolbar_location = None
p.xaxis.axis_label = 'Age'
p.yaxis.axis_label = 'Count'
show(p)
答案 0 :(得分:0)
要使@xxx
起作用,您需要将数据源传递给字形方法。我不确定$xxx
是否应该在没有数据源的情况下工作,或者它是quad
字形的特殊性。
以下是您可以这样做的方法:
# After you bin your data
h_data = pd.DataFrame({'Count': hist, 'Bin': edges[1:]}) # Adjust the edges selection as desired
# HoverTool definition
hover = HoverTool( tooltips="""
<div style ="border-style: solid;border-width: 15px;border-color: gray;background-color:gray;padding:0">
<div>
<span style="font-size: 12px; color: white;font-family:century gothic;">@Count</span>
<span style="font-size: 12px; color: white;font-family:century gothic;"> Observations</span>
</div>
</div style>
""")
# Instead of the quad call
p.vbar(x='Bin', width=2.75, top='Count', bottom=0, line_color='white', line_width=1, color='gray', fill_alpha=0.50, source=h_data)
您也可以使用低级quad
调用来执行此操作,但需要创建一个包含3列的数据源(左右边缘,底部仍可以是常量0)。 vbar
更明确,您甚至可以改进“Bin”列来表示间隔而不是右边(或左边缘)。
请注意,有一些拼写错误(我认为或者您可能使用较旧版本的散点图),p.title_text_font_style
应该是p.title.text_font_style
,而另外一个也应该是window
。