将自定义图例添加到bokeh Bar [英] Add custom legend to bokeh Bar
问题描述
我的熊猫系列为:
>>> etypes
0 6271
1 6379
2 399
3 110
4 4184
5 1987
我想在散景中绘制条形图: p = Bar(etypes)
.但是对于图例,我只得到了 etypes
索引号,我试图使用此字典对其进行解密:
And I want to draw Bar chart in Bokeh: p = Bar(etypes)
. However for legend I get just etypes
index number, which I tried to decrypt with this dictionary:
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())
无效.
by passing it to label argument: p = Bar(etypes, label=legend)
, but it didn't work. Also passing the list(legend.values())
does not work.
有什么想法如何在bokeh条形图中的熊猫系列上添加自定义图例?
Any ideas how to add custom legend on pandas series in bokeh Bar chart?
推荐答案
* Bokeh项目维护者的注意事项:此答案涉及过时且已弃用的API.有关使用现代且完全受支持的Bokeh API创建条形图的信息,请参阅其他响应.
*Note from Bokeh project maintainers: This answer refers to an obsolete and deprecated API. For information about creating bar charts with modern and fully supported Bokeh APIs, see the other response.
将系列转换为DataFrame,将图例添加为新列,然后在引号中引用该列名称作为标签.例如,如果您调用DataFrame'etypes',数据列'values'和图例列'legend':
Convert the series to a DataFrame, add the legend as a new column and then reference that column name in quotes for label. For example, if you call your DataFrame 'etypes', data column 'values', and your legend column 'legend':
p = Bar(etypes, values='values', label='legend')
如果绝对必须使用序列,则可以将序列传递到数据对象,然后将其传递给bokeh.例如:
If you absolutely must use a series, you can pass the series into a data object and then pass that to bokeh. For example:
legend = ['type1', 'type2', 'type3', 'type4', 'type5', 'type6']
data = {
'values': etypes
'legend': legend
}
p = Bar(data, values='values', label='legend')
这篇关于将自定义图例添加到bokeh Bar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!