散景图例找不到字段

Bokeh legend can't find field

我在 Jupyter notebook 中用 bokeh(以前从未使用过这个库)绘制折线图,​​我正在尝试添加图例,但出现以下错误:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

代码

d = {'col1': [1, 2], 'col2': [3, 4], 'label' : ['something', 'something']}
df = pd.DataFrame(data=d)


trend = figure( tools="pan,box_zoom,reset,save",title="trends")

trend.line(source = df, x ='col1', y = 'col2', line_color="red", legend ='label')

show(p)

到目前为止,我已尝试移动 legend 字段并指定 dataframe 名称。

当 DataFrame 作为 source 参数传递时,这些似乎实际上是 figure 中的一个小错误。在这种情况下,DataFrame 在内部自动转换为 Bokeh ColumnDataSource,但显然不会很快发生。但是,修复很简单,因为您可以自己创建 ColumnDataSource

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import output_file, show

d = {'col1': [1, 2], 'col2': [3, 4], 'label' : ['something', 'something']}
df = pd.DataFrame(data=d)

p = figure( tools="pan,box_zoom,reset,save",title="trends")

source = ColumnDataSource(df)
p.line(source=source, x ='col1', y = 'col2', line_color="red", legend ='label')

show(p)

请在 GitHub issue tracker 上使用此代码提交错误报告。