散景图中图例中的逐字标签

Verbatim labels in legend in bokeh plots

我正在尝试使用 python 中的 bokeh 对我的绘图进行交互式分析。

我的数据存储在pandas.Dataframe。我想要一个以列名作为标签的图例。但是,bokeh 而是从相应的列中提取值。

import pandas as pd

from bokeh.plotting import figure
from bokeh.io import output_notebook, show
from bokeh.models import ColumnDataSource

output_notebook()

BokehJS 0.12.13 成功加载。

df = pd.DataFrame({'accuracy': np.random.random(10)}, index=pd.Index(np.arange(10), name='iteration'))
df

输出:

    accuracy
iteration   
0   0.977427
1   0.057319
2   0.307741
3   0.127390
4   0.662976
5   0.313618
6   0.214040
7   0.214274
8   0.864432
9   0.800101

现在剧情:

p = figure(width=900, y_axis_type="log")

source = ColumnDataSource(df)
p.line(x='iteration', y='accuracy', source=source, legend='accuracy')
show(p)

结果:

期望的输出,通过添加space获得:legend='accuracy'+' '

虽然我达到了目的,但方法并不令我满意。我认为,应该有更优雅和官方的方式来区分列名和图例标签。

有。在大多数情况下,Bokeh 会尝试 "do the right thing",但这样做会导致一些行为不太理想的极端情况,这就是其中之一。但是,特别是在这种情况下,您始终可以明确说明字符串是否要解释为 value or as field:

from bokeh.core.properties import value

p.line(x='iteration', y='accuracy', source=source, legend=value('accuracy'))