【问题标题】:Verbatim labels in legend in bokeh plots散景图中图例中的逐字标签
【发布时间】:2018-01-27 01:23:07
【问题描述】:

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

结果:

所需的输出,通过添加空格获得:legend='accuracy'+' '

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

【问题讨论】:

    标签: python pandas bokeh


    【解决方案1】:

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

    from bokeh.core.properties import value
    
    p.line(x='iteration', y='accuracy', source=source, legend=value('accuracy'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2014-07-30
      • 2014-09-08
      • 2019-07-08
      • 1970-01-01
      • 2017-05-25
      相关资源
      最近更新 更多