python - 将自定义图例添加到 Bokeh 栏

标签 python pandas bokeh

我有 pandas 系列作为:

>>> etypes
0    6271
1    6379
2     399
3     110
4    4184
5    1987

我想在 Bokeh 中绘制条形图:p = Bar(etypes)。然而,对于图例,我只得到 etypes 索引号,我试图用这个字典解密它:

legend = {
    0: 'type_1',
    1: 'type_2',
    2: 'type_3',
    3: 'type_4',
    4: 'type_5',
    5: 'type_6',
}

通过将其传递给标签参数:p = Bar(etypes, label=legend),但它不起作用。同时传递 list(legend.values()) 也不起作用。

关于如何在 Bokeh 条形图中的 pandas 系列添加自定义图例有什么想法吗?

最佳答案

*Bokeh 项目维护者的注意事项:此答案指的是一个过时且已弃用的 API。有关使用现代且完全受支持的 Bokeh API 创建条形图的信息,请参阅其他回复。


将系列转换为 DataFrame,将图例添加为新列,然后在标签的引号中引用该列名称。例如,如果您将 DataFrame 称为“etypes”,将数据列称为“values”,将图例列称为“legend”:

p = Bar(etypes, values='values', label='legend') 

如果您绝对必须使用系列,您可以将系列传递到数据对象中,然后将其传递给 bokeh。例如:

legend = ['type1', 'type2', 'type3', 'type4', 'type5', 'type6']
data = {
    'values': etypes
    'legend': legend
}
p = Bar(data, values='values', label='legend')

关于python - 将自定义图例添加到 Bokeh 栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731491/

相关文章:

python - 将 JSON 转换为 R 数据框

python - 检查 url 是否与网站相关

python - Pandas :AttributeError: 'module' 对象没有属性 '__version__'

python - 如何跳过 Bokeh 图中的数据点?

python - TensorFlow 在 session 启动时分配大量主内存

python - 在 Windows 上使用 python 自动 NTLM

python - YouCompleteMe 和 Pyenv 虚拟环境

python - 如何在 pandas 中处理带有超链接/url 的 excel 文件?

python - 访问全息 View 中的 Bokeh (图形)参数

python - 如何在 python 中创建交互式绘图,根据我单击的位置生成新绘图?