在散景图中的传说中的逐字标签

时间:2018-01-26 14:36:43

标签: python pandas bokeh

我正在尝试在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)

结果: enter image description here

通过添加空格获得的所需输出:legend='accuracy'+' 'enter image description here

虽然我达到了目标,但这种方法并不能让我满意。我认为,在列名和图例标签之间应该有更优雅和官方的方式。

1 个答案:

答案 0 :(得分:3)

有。 Bokeh试图在大多数情况下“做正确的事情”,但这样做会导致行为不太理想的一些极端情况,这就是其中之一。但是,特别是在这种情况下,您始终可以明确表示字符串是被解释为value还是field

from bokeh.core.properties import value

p.line(x='iteration', y='accuracy', source=source, legend=value('accuracy'))
相关问题