[英]Highcharts drilldown chart: Display/Hide Plot Bands and Lines on drilldown or drillup
[英]How to interactively display and hide lines in a Bokeh plot?
能夠以交互方式顯示和隱藏散景圖中的線條會很不錯。 說,我創建了這樣的情節:
from bokeh.plotting import output_file, figure, show
from numpy.random import normal, uniform
meas_data_1 = normal(0, 1, 100)
meas_data_2 = uniform(-0.5, 0.5, 100)
output_file("myplot.html", title="My plot")
fig = figure(width=500, plot_height=500)
fig.line(x=range(0, len(meas_data_1)), y=meas_data_1)
fig.line(x=range(0, len(meas_data_2)), y=meas_data_2)
show(fig)
如何添加以交互方式啟用/禁用兩條線之一的可能性?
我知道這是在願望清單上(請參閱此功能請求 ),但這聽起來不會太快實施。
我的印象是使用CheckBoxGroup和自定義的回調應該是可能的,但不幸的是這個回調必須用JavaScript編寫,我完全沒有經驗。
編輯 :從Bokeh 0.12.5
,交互式圖例現已內置到庫中,請參閱https://bokeh.github.io/blog/2017/4/5/release-0-12-5/
這似乎有望在某些時候作為交互式傳說實現: https : //github.com/bokeh/bokeh/issues/3715
目前(v0.12.1),有一個例子在復選框上使用CustomJS來實現這個目的: https : //github.com/bokeh/bokeh/pull/4868
相關代碼:
import numpy as np
from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
from bokeh.models import CheckboxGroup, CustomJS
output_file("line_on_off.html", title="line_on_off.py example")
p = figure()
props = dict(line_width=4, line_alpha=0.7)
x = np.linspace(0, 4 * np.pi, 100)
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend="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),
lang="coffeescript", code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
l2.visible = 2 in checkbox.active;
""")
layout = row(checkbox, p)
show(layout)
我可能錯了,但在我看來,各種行沒有可用的id,隱藏它們的一種方法就是做一個document.getElementById("idSelected").style.visibility = "hidden";
由於CheckBoxGroup
沒有可用的回調,我決定使用Select
。 我可以在CustomJS
獲取所選源代碼,但這幾乎是全部:
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource, Select
from bokeh.plotting import output_file, figure, show
from numpy.random import normal, uniform
meas_data_1 = normal(0, 1, 10)
meas_data_2 = uniform(-0.5, 0.5, 10)
x1 = range(0, len(meas_data_1))
y1 = meas_data_1
source1 = ColumnDataSource(data=dict(x=x1, y=y1))
x2 = range(0, len(meas_data_2))
y2 = meas_data_2
source2 = ColumnDataSource(data=dict(x=x2, y=y2))
output_file("myplot.html", title="My plot")
fig = figure(width=500, plot_height=500)
fig.line('x', 'y', source=source1, line_width=3, line_alpha=0.6)
fig.line('x', 'y', source=source2, line_width=3, line_alpha=0.6)
select = Select(title="Option:", options=["source1", "source2"])
select.callback = CustomJS(args=dict(source1=source1, source2=source2, select=select), code="""
console.log(select.attributes.value);
""")
show(vform(fig, select))
也許您可以“調整” CustomJS
的數據,使其全部為0,具體取決於所選內容,或者如果您可以訪問line_width屬性並使其為0,但這幾乎是我能想到的全部內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.