最佳答案
这是一个例子:
进口:
from bokeh.models import ColumnDataSource, Legend, CustomJS
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
import numpy as np
output_notebook()
抽奖代码:
x = np.linspace(0, 4*np.pi, 100)
y1 = np.sin(x)
y2 = y1 + 1.2
y3 = 0.1 * x**2
fig = figure(plot_height=250)
source = ColumnDataSource(data=dict(x=x, y1=y1, y2=y2, y3=y3))
line1 = fig.line("x", "y1", source=source, legend="Y1", color="red", line_width=3)
line2 = fig.line("x", "y2", source=source, legend="Y2", color="green", line_width=3)
line3 = fig.line("x", "y3", source=source, legend="Y3", color="blue", line_width=3)
legend = fig.legend[0]
legend.click_policy = "hide"
def callback(fig=fig, legend=fig.legend[0]):
y_range = fig.y_range
y_range.have_updated_interactively = False
y_range.renderers = [item.renderers[0] for item in legend.items if item.renderers[0].visible]
Bokeh.index[fig.id].plot_canvas_view.update_dataranges()
for item in legend.items:
item.renderers[0].js_on_change("visible", CustomJS.from_py_func(callback))
show(fig)
结果:
http://nbviewer.jupyter.org/gist/ruoyu0088/8e2d5fb768ee837d3cb59943f944c61f
关于python-3.x - Bokeh:隐藏图例上的某些内容时更新缩放图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48165349/