10

I'm trying to add legend to a figure, which contains two lines created by multi_line method. Example:

p = figure(plot_width=300, plot_height=300)
p.multi_line(xs=[[4, 2, 5], [1, 3, 4]], ys=[[6, 5, 2], [6, 5, 7]], color=['blue','yellow'], legend="first")

In this case the legend is only for the first line. When the legend is defined as a list there is an error:

p.multi_line(xs=[[4, 2, 5], [1, 3, 4]], ys=[[6, 5, 2], [6, 5, 7]], color=['blue','yellow'], legend=["first","second"])

Is it possible to add legend to many lines?

user187205
  • 330
  • 1
  • 2
  • 16

3 Answers3

13

Maintainer Note : PR #8218 which will be merged for Bokeh 1.0, allows legends to be created directly for multi line and patches, without any looping.


To make it faster, when you have a lot of data or a big table etc. You can make a for loop:

1) Make a list of colors and legends

You can always import bokeh paletts for your colors
from bokeh.palettes import "your palett"
Check this link: bokeh.palets

colors_list = ['blue', 'yellow']
legends_list = ['first', 'second']
xs=[[4, 2, 5], [1, 3, 4]]
ys=[[6, 5, 2], [6, 5, 7]]

2) Your figure

p = figure(plot_width=300, plot_height=300)

3) Make a for loop throgh the above lists and show

for (colr, leg, x, y ) in zip(colors_list, legends_list, xs, ys):
    my_plot = p.line(x, y, color= colr, legend= leg)
    
show(p)
Community
  • 1
  • 1
Leo
  • 416
  • 5
  • 7
11

Maintainer Note: PR #8218 which will be merged for Bokeh 1.0, allows legends to be created directly for multi line and patches, without any looping or using separate line calls.


multi_line is intended for conceptually single things, that happen to have multiple sub-components. Think of the state of Texas, it is one logical thing, but it has several distinct (and disjoint) polygons. You might use Patches to draw all the polys for "Texas" but you'd only want one legend overall. Legends label logical things. If you want to label several lines as logically distinct things, you will have to draw them all separately with p.line(..., legend_label="...")

Sabar
  • 478
  • 2
  • 20
bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • 1
    how do you set colors then? If I pass unique colors to multiple ``line(...)`` calls and then ``show(...)`` them, it uses the last color set for all of the lines plotted, making them indistinguishable from one another. – JesseBuesking Oct 08 '15 at 16:51
  • 2
    `line` creates single lines (each composed of multiple points) If you want multiple lines, call `line` for each line you want (can then give each line its own color, dash, line width, etc) You can then also give each line a `legend` property which will cause it to show up as an individual legend item. See, e.g. http://bokeh.pydata.org/en/latest/docs/gallery/legend.html – bigreddot Oct 10 '15 at 20:48
6

On more recent releases (since 0.12.15, I think) its possible to add legends to multi_line plots. You simple need to add a 'legend' entry to your data source. Here is an example taken from the Google Groups discussion forum:

data = {'xs': [np.arange(5) * 1, np.arange(5) * 2],
        'ys': [np.ones(5) * 3, np.ones(5) * 4],
        'labels': ['one', 'two']}

source = ColumnDataSource(data)

p = figure(width=600, height=300)
p.multi_line(xs='xs', ys='ys', legend='labels', source=source)
masta-g3
  • 1,202
  • 4
  • 17
  • 27
  • 1
    Currently (version 2.3.3), the `legend` keyword is deprecated in favour of `legend_group`, `legend_field` and `legend_label`. Only one of these is allowed per call. https://docs.bokeh.org/en/latest/docs/reference/plotting.html#bokeh.plotting.Figure.multi_line – Cecilya Sep 06 '21 at 13:50