散景:与图例标签文本交互

Eri*_*alk 5 python bokeh

有没有办法以交互方式更改 Bokeh 中的图例标签文本?

我已阅读https://github.com/bokeh/bokeh/issues/2274以及如何在散景图中以交互方式显示和隐藏线条?但两者都不适用。

我不需要修改颜色或任何比更改标签文本更复杂的东西,但我找不到办法做到这一点。

Nil*_*lox 4

我希望这个答案可以帮助其他有类似问题的人。

此问题有一个解决方法:从版本 0.12.3 开始,您的图例可以通过用于生成给定元素的 ColumnDataSource 对象进行动态修改。例如:

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)
Run Code Online (Sandbox Code Playgroud)

然后您应该能够通过再次设置类别值来更新图例,例如:

# must have the same length
source_points.data['category'] = ['stack', 'flow', 'stack', 'flow', 'stack', 'flow']
Run Code Online (Sandbox Code Playgroud)

category注意和之间的关系color。如果你有这样的事情:

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']
    ))
Run Code Online (Sandbox Code Playgroud)

然后第二个hi也会显示为蓝色。它仅匹配第一次出现的情况。