Bokeh/Python:如何更改CustomJS调用中的数据列

2024-04-24 03:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

假设源代码有4列,名为time_stime_mintime_hy。在

source = ColumnDataSource(...) 

然后从这些列中首先选择time_s和{}

^{pr2}$

在CustomJS回调代码块中,我希望将x列从time_s切换到time_min,并相应地重新呈现绘图。我知道如何通过在DevTools控制台中分析来浏览/访问一些数据和对象,但是我无法从p.line对象中找到x='time_s',也无法找到如何访问它们。在

js = """
// Explore Objects in DevTools Console
console.log(data)
console.log(source)
console.log(plot) 

data = source.data
data['time_s'] = do something to column here ...

if (widget.value === 'xyz') {
    // Looking for something like:
    plot.line.x = data['time_min'] // not working
}

// After data change -> rerender
source.change.emit()

"""
cb = CustomJS(args=dict(src=source, plot=p, widget=mywidget), code=js)
mywidget.js_on_change('value', cb)

那么如何才能做到这一点呢?在


Tags: 对象logsourcedatatimeplotlinejs
1条回答
网友
1楼 · 发布于 2024-04-24 03:34:13

我可以想象有很多种方法可以做到这一点。假设您可以容忍在数据源中复制一列,我建议您这样做:

import numpy as np

from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Select
from bokeh.plotting import figure

x = np.linspace(0, 10, 100)
foo = x**2
bar = np.sin(x)

source = ColumnDataSource(data=dict(x=x, y=foo, foo=foo, bar=bar))

plot = figure(plot_height=350)
plot.line(x='x', y='y', source=source)

select = Select(value='foo', options=['foo', 'bar'])
select.js_on_change('value', CustomJS(args=dict(source=source, select=select), code="""
    // make a shallow copy of the current data dict
    const new_data = Object.assign({}, source.data)

    // update the y column in the new data dict from the appropriate other column
    new_data.y = source.data[select.value]

    // set the new data on source, BokehJS will pick this up automatically
    source.data = new_data
"""))

show(column(plot, select))

如果不能将选定的列复制到“y”列中,则可以更新glyph,以更改它指向的列。看起来像这样:

^{pr2}$

相关问题 更多 >