有没有一种方法可以以交互方式关闭python Bokeh图中的图例 [英] Is there a way to interactively turn off the legend in a python `Bokeh` plot

查看:71
本文介绍了有没有一种方法可以以交互方式关闭python Bokeh图中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bokeh图中隐藏图例

所以我知道谁可以以编程方式关闭 Bokeh 情节中的图例,但是我想知道是否有一种交互式的方式来做到这一点?有时我在一个地块图例中有很多项目,并且图例占用了大量空间或不动产.我想知道是否可以通过单击图例将其隐藏,还是可以使用某些这样的选项?

So I understand who to programmatically turn off the legend in a Bokeh plot, however I was wondering if there is a way to do this interactively? Sometimes I have a number of items in a plot legend, and the legend takes up lot of space or real estate. I was wondering if there is a way to click on the legend to hide it, or some such option?

我知道我可以通过代码影响图例的可见性:

I know I can affect the legend visibility through the code:

 myPlot.legend.visible = False

不过,我将能够根据需要打开和关闭图例.

However I would to be able to turn the legend on and off as I wish.

推荐答案

您可以使用 CustomJS ,这是在 DoubleTap 事件上切换图例的示例:

You can use CustomJS, here is an example that toggle legend on DoubleTap event:

import numpy as np
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
from bokeh import events
from bokeh.models import CustomJS, ColumnDataSource

t = np.linspace(0, 4*np.pi, 100)
source = ColumnDataSource(data=dict(x=t, y1=np.sin(t), y2=np.cos(t)))
fig = figure(plot_height=250)
fig.line("x", "y1", source=source, legend="sin", line_color="red")
fig.line("x", "y2", source=source, legend="cos", line_color="green")

def show_hide_legend(legend=fig.legend[0]):
    legend.visible = not legend.visible

fig.js_on_event(events.DoubleTap, CustomJS.from_py_func(show_hide_legend))

show(fig)

这篇关于有没有一种方法可以以交互方式关闭python Bokeh图中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆