Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh: Interact with legend label text

Tags:

python

bokeh

Is there any way to interactively change legend label text in Bokeh?

I've read https://github.com/bokeh/bokeh/issues/2274 and How to interactively display and hide lines in a Bokeh plot? but neither are applicable.

I don't need to modify the colors or anything of more complexity than changing the label text but I can't find a way to do it.

like image 918
Eric Kaschalk Avatar asked Aug 20 '16 01:08

Eric Kaschalk


1 Answers

I hope this answer can help others with similar issues.

There is a workaround to this problem: starting from version 0.12.3 your legends can be dynamically modified through a ColumnDataSource object used to generate the given elements. For example:

source_points = ColumnDataSource(dict(
                x=[1, 2, 3, 4, 5, 6],
                y=[2, 1, 2, 1, 2, 1],
                color=['blue','red','blue','red','blue','red'],
                category=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
                ))
self._figure.circle('x',
                    'y',
                    color='color',
                    legend='category',
                    source=source_points)

Then you should be able to update the legend by setting the category values again, like:

# must have the same length
source_points.data['category'] = ['stack', 'flow', 'stack', 'flow', 'stack', 'flow']

Note the relation between category and color. If you had something like this:

source = ColumnDataSource(dict(
        x=[1, 2, 3, 4, 5, 6],
        y=[2, 1, 2, 1, 2, 1],
        color=['blue','red','blue','red','blue','red'],
        category=['hi', 'hi', 'hi', 'lo', 'hi', 'lo']
    ))

Then the second hi would show up blue as well. It only matches the first occurrence.

like image 108
Gabriel Avatar answered Sep 29 '22 23:09

Gabriel