python - 将悬停工具提示添加到 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, 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 )   

其中有效列是我要在 pandas 数据框中检查的所有列的列表。我正在尝试添加一个悬停工具提示,它会显示每个箱子中存储的元素数量,但我无法这样做。任何帮助,将不胜感激。

最佳答案

如果您不想使用 ColumnDataSource ,您可以将 @data_value 替换为 @top 并且它应该只需最少的编辑即可工作:

hover = HoverTool(tooltips = [('Value', '@top')])
p.add_tools(hover)

即编辑示例 histogram.py这种方式也有效:

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

关于python - 将悬停工具提示添加到 Bokeh 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28882528/

相关文章:

python - 在 django 模板中渲染动态变量

html - 菜单一直是 "clicked"。悬停效果出错

ruby-on-rails - 我想向字形图标添加工具提示

Python: mechanize has no attribute 'TextControl' 错误

python - 无法超越 JSON 文件中的第一个字典

python - 在使用管理面板插入模型字段之前对其进行预处理

javascript - Bootstrap 的导航下拉切换问题

css - 当鼠标悬停在芒果上时,我想显示绿色的苹果而不是红色的芒果

java - 如何将图像工具提示添加到 JavaFX 中的 ComboBox 项目?

jquery - JQPlot 折线图上的自定义工具提示