javascript - 使用 Bokeh 单击按钮从文件加载图形数据

标签 javascript python file plot bokeh

我正在尝试使用 Bokeh 中的切换按钮来创建一个交互式网站,用户可以在其中单击切换按钮来选择要绘制的图表。

按钮将从文本文件(包含两列 x 和 y 数据)加载数据。数据文件有两列包含由空格分隔的 x 和 y 数据。

选择切换按钮时,将绘制相应的数据,当取消切换按钮时将删除该图。

我目前在向回调事件传递参数时遇到问题,这可能吗?

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)

最佳答案

你应该加载这两个文件并将数据保存到一个DataSource对象中,这是一个例子:

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)

关于javascript - 使用 Bokeh 单击按钮从文件加载图形数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680708/

相关文章:

javascript - 这段 JS 代码是如何工作的,它是以什么形式编码的?

python - 我应该如何在我的模板之间共享代码 ("modules")?

python - 使用 Python [摘要] 读取 wav 文件的最简单方法是什么?

python - Firebase Python firestore.DELETE_FIELD 在 VS Code 中显示为错误

C 从文件中读取负数和正数到数组中

Clojure 中带有 ByteBuffer 的 Java FileChannel?

javascript - 动态内容均匀分布在 2 列之间

javascript - vue.js mapbox map 未显示

javascript - 使用简单的 jison 语法解析错误

java - ImageIcon 的 Mac 文件路径