pythonbokeh

In Bokeh, how can I update the color of lines when updating a multiline data source?


I want to update a multi_line in a bokeh figure. As part of the updates, I need to adjust colors.

First, I make a simple figure:

from bokeh.plotting import figure, show

p = figure()
# create a dummy multiline so that we can update it later with new data
p.multi_line(xs=[[0,1]], ys=[[0,1]], name='my_lines')

show(p)

This gives fine results:

enter image description here

Then I try to update the lines:

lines_ds = p.document.get_model_by_name('my_lines').data_source

lines_ds.data = dict(
    xs=[[1,2,3],[0,0]],
    ys=[[0,1,0],[0,1]],
    color=['black','red']
)
show(p)

This updates my x and y data, but not the colors:

enter image description here

How can I update the colors of the lines? I already tried colors instead of color. I'm not entirely opposed to simply creating an entirely new figure each time I need to update (multiple times per second), but I've been told that the right way to use bokeh is to update your objects rather than deleting and recreating things.


Solution

  • I found my own solution. I needed to change 2 things. First, line_color needed to be part of the original creation of the multi_line (alternatively, you could use color instead of line_color here):

    from bokeh.plotting import figure, show
    
    p = figure()
    # create a dummy multiline so that we can update it later with new data
    p.multi_line(xs=[[0,1]], ys=[[0,1]], name='my_lines', line_color=['red'])
    
    show(p)
    

    Second, I needed to change color to line_color when making the update:

    lines_ds = p.document.get_model_by_name('my_lines').data_source
    
    lines_ds.data = dict(
        xs=[[1,2,3],[0,0]],
        ys=[[0,1,0],[0,1]],
        line_color=['black','red']
    )
    show(p)