【问题标题】:Bokeh legend can't find field散景图例找不到字段
【发布时间】:2018-01-10 15:28:25
【问题描述】:

我在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 名称。

【问题讨论】:

    标签: python-2.7 jupyter-notebook bokeh


    【解决方案1】:

    当 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 上提交包含此代码的错误报告。

    【讨论】:

    • 谢谢,将在 GitHub 上报告错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2022-10-17
    相关资源
    最近更新 更多