使用 CustomJS 向散景图添加交互式图块
Adding an interactive tile to a Bokeh plot using CustomJS
我有一个交互式散景图,它使用 CheckBoxGroup 来绘制不同的组合。我希望每次更改组合时都能修改标题,而不必在代码中更改标题并再次生成绘图。
我对 CustomJS 非常熟悉,但我尝试了以下方法(...只有标题摘录):
p = figure(title = 'Default title that appears',
toolbar_location = 'right')
.
.
update_title = CustomJS(args =dict(source=source, text = text),
code ="""
const data = source.data;
var text = text.value;
text = text.value;
source.change.emit();
""")
text_group = TextInput(title = 'Title', value = "Default value", callback = update_title)
show(column(p,checkbox_group, text_group))
我得到的结果显示了一个标题框,但当我更改标题时没有任何反应。
I essentially want to do this:
但使用 CustomJS 而不是 Python 回调。
您只需将标题传递给回调即可。我发现情节标题是一个散景模型,所以所有常规操作都适用。您的回调可能如下所示:
update_title = CustomJS(args=dict(title=plot.title), code="""
title.text = cb_obj.value
""")
完整的例子是
import numpy as np
from bokeh.layouts import row, column
from bokeh.models import CustomJS, TextInput
from bokeh.plotting import figure, output_file, show, ColumnDataSource
x = np.linspace(0, 10, 500)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(y_range=(-10, 10), plot_width=400, plot_height=400, title='my sine wave')
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
update_title = CustomJS(args=dict(title=plot.title), code="""
title.text = cb_obj.value
""")
text = TextInput(title='Enter title', value='my sine wave', callback=update_title)
layout = row(
plot,
text
)
output_file('title_change.html', title='change title')
show(layout)
我有一个交互式散景图,它使用 CheckBoxGroup 来绘制不同的组合。我希望每次更改组合时都能修改标题,而不必在代码中更改标题并再次生成绘图。
我对 CustomJS 非常熟悉,但我尝试了以下方法(...只有标题摘录):
p = figure(title = 'Default title that appears',
toolbar_location = 'right')
.
.
update_title = CustomJS(args =dict(source=source, text = text),
code ="""
const data = source.data;
var text = text.value;
text = text.value;
source.change.emit();
""")
text_group = TextInput(title = 'Title', value = "Default value", callback = update_title)
show(column(p,checkbox_group, text_group))
我得到的结果显示了一个标题框,但当我更改标题时没有任何反应。
I essentially want to do this:
但使用 CustomJS 而不是 Python 回调。
您只需将标题传递给回调即可。我发现情节标题是一个散景模型,所以所有常规操作都适用。您的回调可能如下所示:
update_title = CustomJS(args=dict(title=plot.title), code="""
title.text = cb_obj.value
""")
完整的例子是
import numpy as np
from bokeh.layouts import row, column
from bokeh.models import CustomJS, TextInput
from bokeh.plotting import figure, output_file, show, ColumnDataSource
x = np.linspace(0, 10, 500)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(y_range=(-10, 10), plot_width=400, plot_height=400, title='my sine wave')
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
update_title = CustomJS(args=dict(title=plot.title), code="""
title.text = cb_obj.value
""")
text = TextInput(title='Enter title', value='my sine wave', callback=update_title)
layout = row(
plot,
text
)
output_file('title_change.html', title='change title')
show(layout)