Can't order legend entries in hbar plot

Gotcha. In case someone comes across this post looking for a solution, the final chunk of code looks like this:

# Define source
source = ColumnDataSource(df[['name', 'count', 'type']])

# Create figure
p = figure(y_range=FactorRange(factors=name, bounds=(0, len(name))))
r = p.hbar(y='name', left=0, right='count', height=0.9, source=source)

# Add legend
legend = Legend(items=[
    LegendItem(label='1', renderers=[r], index=6),
    LegendItem(label='2', renderers=[r], index=37),
    LegendItem(label='3', renderers=[r], index=1),
    LegendItem(label='4', renderers=[r], index=10),
    LegendItem(label='5', renderers=[r], index=2),
    LegendItem(label='6', renderers=[r], index=14),
    LegendItem(label='7', renderers=[r], index=8),
], title='Type', location='bottom_right')
p.add_layout(legend)

where the indexes were obtained manually (can probably be done programatically).

1 Like