how to show legend items of patches in bokeh

穿精又带淫゛_ 提交于 2019-12-02 18:37:52

问题


In the following set up, I create a area chart based on the basic example. How do I get the legend for my input automatically or even programatically. For now I get only legend with one item 'a' and the first color.

from bokeh.plotting import *

...
patches([x2 for a in areas], list(areas.values()), color=colors, alpha=0.8,
    line_color=None, legend='a', title="hello chart")

legend().orientation = "top_right" # what other options, may here?
show()

What is the format to pass into patches for the legend as value or how can I trigger the legend() to show an item and color for every item in the plot?


回答1:


I don't have an answer using patches, but you can use multiple patch:

from bokeh.plotting import *

...
for a, area in enumerate(areas):
    p.patch(x2, areas[area], color=colors[a], legend=area, alpha=0.8, line_color=None)

show()

which shows the legend for each area nicely.




回答2:


I found the following comment in bokeh and stay tuned:

OK, these hand drawn legends are pretty clunky, will be improved in future release

This is working for now:

hold() # stop the curplot() 
# and add the legend just next to the data
x, y = 15.5, 0
for i,area in enumerate(areas):
    rect([x], [y], color=colors[i], width=0.3, height=400)
    text([x], [y], text=area, angle=0, text_font_size="8pt", text_align="center", text_baseline="middle")
    y = y + 100

show()


来源:https://stackoverflow.com/questions/25295853/how-to-show-legend-items-of-patches-in-bokeh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!