我使用以下代码在 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/