Skip to content Skip to sidebar Skip to footer

Bokeh Position Legend Outside Plot Area For Stacked Vbar

I have a stacked vbar chart in Bokeh, a simplified version of which can be reproduced with: from bokeh.plotting import figure from bokeh.io import show months = ['JAN', 'FEB', 'MA

Solution 1:

For anyone interested, have now fixed this using simple indexing of the vbar_stack glyphs. Solution below:

from bokeh.plotting import figure
from bokeh.io import show
from bokeh.models import Legend

months = ['JAN', 'FEB', 'MAR']
categories = ["cat1", "cat2", "cat3"]
data = {"month" : months,
        "cat1"  : [1, 4, 12],
        "cat2"  : [2, 5, 3],
        "cat3"  : [5, 6, 1]}
colors = ["#c9d9d3", "#718dbf", "#e84d60"]

p = figure(x_range=months, plot_height=250, title="Categories by month",
           toolbar_location=None)
v = p.vbar_stack(categories, x='month', width=0.9, color=colors, source=data)

legend = Legend(items=[
    ("cat1",   [v[0]]),
    ("cat2",   [v[1]]),
    ("cat3",   [v[2]]),
], location=(0, -30))

p.add_layout(legend, 'right')

show(p)

Solution 2:

Thanks Toby Petty for your answer.

I have slightly improved your code so that it automatically graps the categories from the source data and assigns colors. I thought this might be handy as the categories are often not explicitly stored in a variable and have to be taken from the data.

from bokeh.plotting import figure
from bokeh.io import show
from bokeh.models import Legend
from bokeh.palettes import brewer

months = ['JAN', 'FEB', 'MAR']
data = {"month" : months,
        "cat1"  : [1, 4, 12],
        "cat2"  : [2, 5, 3],
        "cat3"  : [5, 6, 1],
        "cat4"  : [8, 2, 1],
        "cat5"  : [1, 1, 3]}
categories = list(data.keys())
categories.remove('month')
colors = brewer['YlGnBu'][len(categories)]

p = figure(x_range=months, plot_height=250, title="Categories by month",
           toolbar_location=None)
v = p.vbar_stack(categories, x='month', width=0.9, color=colors, source=data)

legend = Legend(items=[(x, [v[i]]) for i, x in enumerate(categories)], location=(0, -30))

p.add_layout(legend, 'right')

show(p)

Post a Comment for "Bokeh Position Legend Outside Plot Area For Stacked Vbar"