使用散景单击按钮从文件加载图形数据
load graph data from files on button click with bokeh
我正在尝试使用 Bokeh 中的切换按钮来创建一个交互式网站,用户可以在其中单击切换按钮以 select 绘制哪些图表。
这些按钮将从文本文件(包含两列 x 和 y 数据)加载数据。数据文件有两列包含由 space.
分隔的 x 和 y 数据
当切换按钮被 selected 时,相应的数据将被绘制,当切换按钮被 deselected 时,该图将被删除。
我目前在向回调事件传递参数时遇到问题,可以吗?
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Toggle
from bokeh.plotting import figure, output_file, show
output_file("load_data_buttons.html")
x = [0]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=filename,dict(source=source), code="""
var data = source.get('data');
console.log(filename)
x = data['x']
y = data['y']
#load data stored in the file name and assign to x and y
source.trigger('change');
""")
toggle1 = Toggle(label="Load data file 1", type="success",callback=callback("data_file_1.txt"))
toggle2 = Toggle(label="Load data file 2", type="success",callback=callback("data_file_2.txt"))
layout = vform(toggle1, toggle2, plot)
show(layout)
你应该加载这两个文件并将数据保存到一个数据源对象中,这里是一个例子:
from bokeh.io import vplot
import pandas as pd
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Button
from bokeh.plotting import figure, output_file, show
output_file("load_data_buttons.html")
df1 = pd.read_csv("data_file_1.txt")
df2 = pd.read_csv("data_file_2.txt")
plot = figure(plot_width=400, plot_height=400)
source = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1]))
source2 = ColumnDataSource(data=dict(x1=df1.x.values, y1=df1.y.values,
x2=df2.x.values, y2=df2.y.values))
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=dict(source=source, source2=source2), code="""
var data = source.get('data');
var data2 = source2.get('data');
data['x'] = data2['x' + cb_obj.get("name")];
data['y'] = data2['y' + cb_obj.get("name")];
source.trigger('change');
""")
toggle1 = Button(label="Load data file 1", callback=callback, name="1")
toggle2 = Button(label="Load data file 2", callback=callback, name="2")
layout = vplot(toggle1, toggle2, plot)
show(layout)
我正在尝试使用 Bokeh 中的切换按钮来创建一个交互式网站,用户可以在其中单击切换按钮以 select 绘制哪些图表。
这些按钮将从文本文件(包含两列 x 和 y 数据)加载数据。数据文件有两列包含由 space.
分隔的 x 和 y 数据当切换按钮被 selected 时,相应的数据将被绘制,当切换按钮被 deselected 时,该图将被删除。
我目前在向回调事件传递参数时遇到问题,可以吗?
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Toggle
from bokeh.plotting import figure, output_file, show
output_file("load_data_buttons.html")
x = [0]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=filename,dict(source=source), code="""
var data = source.get('data');
console.log(filename)
x = data['x']
y = data['y']
#load data stored in the file name and assign to x and y
source.trigger('change');
""")
toggle1 = Toggle(label="Load data file 1", type="success",callback=callback("data_file_1.txt"))
toggle2 = Toggle(label="Load data file 2", type="success",callback=callback("data_file_2.txt"))
layout = vform(toggle1, toggle2, plot)
show(layout)
你应该加载这两个文件并将数据保存到一个数据源对象中,这里是一个例子:
from bokeh.io import vplot
import pandas as pd
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Button
from bokeh.plotting import figure, output_file, show
output_file("load_data_buttons.html")
df1 = pd.read_csv("data_file_1.txt")
df2 = pd.read_csv("data_file_2.txt")
plot = figure(plot_width=400, plot_height=400)
source = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1]))
source2 = ColumnDataSource(data=dict(x1=df1.x.values, y1=df1.y.values,
x2=df2.x.values, y2=df2.y.values))
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=dict(source=source, source2=source2), code="""
var data = source.get('data');
var data2 = source2.get('data');
data['x'] = data2['x' + cb_obj.get("name")];
data['y'] = data2['y' + cb_obj.get("name")];
source.trigger('change');
""")
toggle1 = Button(label="Load data file 1", callback=callback, name="1")
toggle2 = Button(label="Load data file 2", callback=callback, name="2")
layout = vplot(toggle1, toggle2, plot)
show(layout)