Python 向bokeh直方图添加悬停工具提示
我使用以下代码在bokeh中创建了一个直方图:Python 向bokeh直方图添加悬停工具提示,python,hover,tooltip,bokeh,Python,Hover,Tooltip,Bokeh,我使用以下代码在bokeh中创建了一个直方图: TOOLS="pan,wheel_zoom,box_zoom,reset,hover" for column in valid_columns: output_file_name = str( file_name + column + ".html" ) data_values = stats[ column ].tolist() output_file( output_file_name ) histogram
TOOLS="pan,wheel_zoom,box_zoom,reset,hover"
for column in valid_columns:
output_file_name = str( file_name + column + ".html" )
data_values = stats[ column ].tolist()
output_file( output_file_name )
histogram, edges = np.histogram( data_values, bins=50 )
source = ColumnDataSource(
data = dict( data_value = data_values ) )
p1 = figure( title = column, background_fill="#E8DDCB", tools=TOOLS )
p1.quad( top = histogram, bottom = 0, left = edges[ :-1 ], right = edges[ 1: ],
fill_color = "#036564", line_color = "#033649" )
hover = p1.select(dict(type=HoverTool))
hover.tooltips = [ ( "Value", "@data_value" ) ]
show( p1 )
print( "Saved Figure to ", output_file_name )
其中,valid columns是我希望在数据框中检查的所有列的列表。我正在尝试添加一个悬停工具提示,它将显示每个箱子中存储的物品数量,但我无法这样做。任何帮助都将不胜感激 看起来您缺少了一些东西:
直方图长度相同的源
,而不是数据值
。更具体地说,我认为您希望您的源代码是:
source = ColumnDataSource( data = dict( data_value = histogram ) )
source
添加到您的p1.quad
调用中,即
p1.quad( top = histogram, bottom = 0, left = edges[ :-1 ], right = edges[ 1: ],
fill_color = "#036564", line_color = "#033649", source = source )
如果您不想使用CD,您可以将
@data\u value
替换为@top
,并且它应该能够在最少的编辑下工作:
hover = HoverTool(tooltips = [('Value', '@top')])
p.add_tools(hover)
i、 e.以这种方式编辑示例同样有效:
from bokeh.models import HoverTool
def make_plot(title, hist, edges, x, pdf, cdf):
p = figure(title=title, tools='', background_fill_color="#fafafa")
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
fill_color="navy", line_color="white", alpha=0.5)
p.line(x, pdf, line_color="#ff8888", line_width=4, alpha=0.7, legend_label="PDF")
p.line(x, cdf, line_color="orange", line_width=2, alpha=0.7, legend_label="CDF")
p.y_range.start = 0
p.legend.location = "center_right"
p.legend.background_fill_color = "#fefefe"
p.xaxis.axis_label = 'x'
p.yaxis.axis_label = 'Pr(x)'
p.grid.grid_line_color="white"
hover = HoverTool(tooltips = [('Density', '@top')])
p.add_tools(hover)
return p
在不久的将来,在用户提供的源代码中混合命名列和自动创建列(从文字数据值)是不可取的。(因为这样做会修改用户提供的源代码,这可能是意外和不需要的)我建议将
顶部
、左侧
和右侧
列也放在源代码中。@bigreddot您如何将顶部
、左侧
和右侧
放在源代码中?在直方图库()中,这些参数仍在使用,如NublicPablo的回答所示,还是我遗漏了什么?如何加载和使用CD在《用户指南》中有介绍: