Bokeh+Python在vbar上悬停,而数据来自pandas

2024-05-23 23:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框,我从中提取数据,并使用Bokeh显示为条形图。我想要的是在悬停时显示每个条的最大值。这是我使用Bokeh的第一天,我已经更改了几次代码,我真的很困惑如何设置它。我补充说:

p.add_tools(HoverTool(tooltips=[("x_ax", "@x_ax"), ("y_ax", "@y_ax")]))

行,但就是不明白

代码如下:

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, ranges, LabelSet
from bokeh.plotting import figure, save, gridplot, output_file

# prepare some data
# x = pd.Series(range(1,36))
x_ax = FAdf['SampleID']
y_ax = FAdf['First Run Au (ppm)']

# output to static HTML file
output_file("bars.html")

# create a new plot with a title and axis labels
p = figure(x_range=x_ax, title="Batch results", x_axis_label='sample', y_axis_label='Au (ppm)',
           toolbar_location="above", plot_width=1200, plot_height=800)

p.add_tools(HoverTool(tooltips=[("x_ax", "@x_ax"), ("y_ax", "@y_ax")]))

# setup for the bars
p.vbar(x=x_ax, top=y_ax, width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0

# turn bar tick labels 45 deg
p.xaxis.major_label_orientation = np.pi/3.5

# show the results
show(p)

来自FAdf数据库的示例:

SampleID:
0                 KR-19  349
1                 KR-19  351
2                    Blank_2
3                 KR-19  353

First Run Au (ppm):
0      0.019
1      0.002
2      0.000
3      0.117

Tags: fromimportoutputplotshowbokehrangeax
1条回答
网友
1楼 · 发布于 2024-05-23 23:03:00

如果您将实际的文字数据序列传递给如上所述的glyph方法,那么Bokeh将使用诸如“x”和“y”之类的通用字段名,因为它无法知道使用的任何其他名称。以下是配置悬停工具所需的列:

tooltips=[("x_ax", "@x"), ("y_ax", "@y")])

或者,您可以将source参数传递给vbar方法,以便列具有您喜欢的列名。用户指南中对此进行了描述:

https://docs.bokeh.org/en/latest/docs/user_guide/data.html

相关问题 更多 >