Python Bokeh,如何从多个图例中隐藏同一行

问题描述 投票:0回答:1

我在这里有这段代码,该代码创建了一个散景图,所有图和平移和缩放在一起。我希望能够在单击line1图例项(或图外的可选其他小部件或复选框)时同时在所有3个图上同时隐藏相同的命名行'line1',而不必单独单击它们。我该怎么办?

每次都可以从不同的数据源创建第1行,即所有行的x值都相同,但是(图0,第1行)和(图1,行1)之间的y值可以不同]

当前输出:

plot with all graphs

想要的输出:

enter image description here

from bokeh.io import output_file, show,save
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource

output_file("panning.html")
data=[]
x = list(range(11))
y0 = x
y1 = [10-xx for xx in x]
y2 = [abs(xx-5) for xx in x]
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1,y2=y2))
for i in range(3):
    p = figure(title="Basic Title", plot_width=300, plot_height=300)
    if len(data):
        p.x_range=data[0].x_range
        p.y_range=data[0].y_range

    p.circle('x', 'y0', size=10, color="navy", alpha=0.5,legend_label='line1', source=source)

    p.triangle('x', 'y1', size=10, color="firebrick", alpha=0.5,legend_label='line2', source=source)

    p.square('x', 'y2', size=10, color="olive", alpha=0.5,legend_label='line3', source=source)
    p.legend.location='top_right'
    p.legend.click_policy = "hide"
    data.append(p)
plot_col=column(data)
# show the results
show(plot_col)

save(plot_col)
python bokeh
1个回答
0
投票

交互式(可单击)图例在多个图上无法协调。您将按照建议使用专用的小部件。

回购中有一个确切的例子:

https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/line_on_off.py

l0 = p.line(x, np.sin(x), color=Viridis3[0], legend_label="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend_label="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend_label="Line 2", **props)

checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"],
                         active=[0, 1, 2], width=100)
checkbox.callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox), code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
l2.visible = 2 in checkbox.active;
""")
© www.soinside.com 2019 - 2024. All rights reserved.