python - 在 Bokeh 中隐藏图例旁边的注释

标签 python pandas plot bokeh

我有一个交互式 Bokeh 图,单击图例时可以隐藏某些圆形图。现在,当我通过单击来禁用绘图时,绘制的圆圈会消失,但注释仍然存在。有人可以向我解释一下如何将它们全部打开/关闭吗?或者有人有快速解决办法吗?

with

这是关闭时的图片:

without

我使用以下代码绘制圆圈+图例:

q.circle('lng', 'lat', source = source2, name='vri', color='red', size=5, hover_line_color="black", legend_label = 'VRI')

vri_labels = LabelSet(x='lng', y='lat', text='kruispuntn', x_offset=5, y_offset=5, source=source2, text_font_size = '10pt')

q.legend.location = "bottom_left"
q.legend.click_policy="hide"

q.add_layout(vri_labels)

show(q)

最佳答案

您可以通过 CustomJS 回调链接 visible 属性:

from bokeh.io import show
from bokeh.models import ColumnDataSource, LabelSet, CustomJS
from bokeh.plotting import figure

p = figure()
cds = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1], z=[1, 0]))

for var, params in [('y', {}),
                    ('z', {'color': 'green'})]:
    renderer = p.circle('x', var, source=cds, legend_label=var, size=20, **params)
    label_set = LabelSet(x='x', y=var, text=var, source=cds, x_offset=5, y_offset=5)
    p.add_layout(label_set)
    renderer.js_on_change('visible', CustomJS(args=dict(ls=label_set),
                                              code="ls.visible = cb_obj.visible;"))

p.legend.click_policy = 'hide'

show(p)

关于python - 在 Bokeh 中隐藏图例旁边的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61210735/

相关文章:

python - 如何获取类中的所有属性?

python - 使用 Groupby 识别 Pandas Dataframe 中连续的相同值

python - Pandas 通过 Compare-cumsum-groupby 模式跟踪连续的邻近数字

java - rCaller 生成一个空图像和空图

以 Octave 为单位绘制混淆矩阵;我如何绘制矩阵本身?

python - matplotlib 文本仅在绘图区域

python - pyqt5 标准化向量

python - request.session 没有将完整的 querydict 传输到另一个 View

python - 如何规范化数据帧的列,忽略其中的无值?

matlab - scatter3 的问题