Bokeh / Python:如何在CustomJS回调中更改数据列

时间:2019-06-09 22:57:33

标签: python callback bokeh

假设源有4个列,分别为...ConfigureServices method services.AddSwaggerGen(opts => { opts.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" }); }); ...Configure method app.UseSwagger(); app.UseSwaggerUI(opts => { opts.SwaggerEndpoint("/swagger/v1/swagger.json", "API"); opts.RoutePrefix = string.Empty; }); time_stime_min     和time_h

y

然后从最初的source = ColumnDataSource(...) time_s列中选择

y

在CustomJS回调代码块中,我想将x列从p = figure(tools=tools, toolbar_location="above") p.line(x='time_s', y='y', source=source) 切换为time_s并相应地重新渲染图。我知道如何通过在DevTools-console中进行分析来大致浏览/访问某些数据和对象,但是我无法从time_min对象中找到x='time_s'或如何访问它们。

p.line

那怎么办呢?

1 个答案:

答案 0 :(得分:0)

我可以想象有多种方式可以做到这一点。假设您可以忍受在数据源中重复的一列,这就是我的建议:

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”列中是不可接受的,则可以 更新字形,以更改其指向的列。看起来像这样:

r = p.line(x='x', y='foo' source=source)

cb = CustomJS(args=dict(r=r, select=select), code="""
    // tell the glyph which field of the source y should refer to
    r.glyph.y.field = select.value

    // manually trigger change event to re-render
    r.glyph.change.emit()
""")