如何在Bokeh中根据熊猫数据框标记各个x轴刻度?

问题描述

我有以下数据框:

       Foo  Bar
A      100. 20. 
B      65.2 78. 

我想将此数据帧绘制为bokeh,因此我有一条线表示Foo,一条线表示Bar,x轴刻度线标记为A和B,而不是0和1。到目前为止,我具有以下:

p = figure()
p.line(df["Foo"],df.index.values)
show(p)

但是这仍然显示x轴刻度为整数,而不是预期的索引值A和B。如何显示索引值?

我也尝试了以下方法

p = figure(x_range=df.index.values)
p.line(df["Foo"])
show(p)

我仍然在图中看不到任何线条。

解决方法

使用bokeh时,棘手的部分是,如果希望将轴归类,则在设置图形时需要在bokeh图上指定它的可能值。

import pandas as pd
from bokeh.plotting import figure
from bokeh.io import show

df = pd.DataFrame({"Foo":[100,65.2],"Bar": [20,78]},index=["A","B"])
print(df)
     Foo  Bar
A  100.0   20
B   65.2   78


# Tell bokeh our plot will have a categorical x-axis 
#  whose values are the index of our dataframe
p = figure(x_range=df.index.values,width=250,height=250)
p.line(x="index",y="Foo",source=df,legend_label="Foo")
p.line(x="index",y="Bar",legend_label="Bar")

p.legend.location = "bottom_right"
show(p)

enter image description here