我正在使用Bokeh(Python)绘制历史上众所周知的气候指数(例如厄尔尼诺现象)的时间序列。
我的图表中的x轴是时间,特别是月/年。 y轴是指数的分析值。
Bokeh中内置的Tap工具允许我选择时间序列中的一个点并使其突出显示。
我希望在该功能之上完成的是拥有一个回调,该回调将突出显示时间序列中索引值等于我通过点击工具选择的点的其他点。
我最大的问题是我根本不懂JavaScript(我只是一个Python-er),所以我想避免花费数小时学习JavaScript只是为了写一个5行回调函数。
任何人都有可能实现此目的的建议或代码段吗?
我真的很感激任何帮助。
答案 0 :(得分:0)
据我了解,要从回调中选择点,您需要更新用于创建绘图的selected
对象的ColumnDataSource
属性,并且也作为参数传递回调。如:
from bokeh.models import ColumnDataSource, TapTool
source = ColumnDataSource(data=dict(x=df.x, y=df.y))
p=figure(tools="reset,tap")
p.circle("x", "y", source=source)
cb_click = CustomJS(args=dict(source=source), code="""
l_selected=source.selected
// create an array idx that contains the indices of the points you want to select
l_selected['1d'].indices=idx
source.selected=l_selected
""")
p.add_tools(TapTool(callback=cb_click))
p.show()
答案 1 :(得分:0)
您还可以为字形编写python回调。以下是圆形字形的示例。通过单击其中一个点,可以选择所有共享相同y值的点。
from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
import pandas as pd
TOOLS = ["tap"]
p = figure(title="Some Figure", tools=TOOLS)
df = pd.DataFrame(data={'x': [1, 2, 3, 4, 5, 6, 7, 8, 9],
'y': [11, 12, 13, 15, 15, 11, 12, 11, 15]
})
source = ColumnDataSource(dict(df))
pglyph = p.circle(x='x', y='y', source=source)
def callback(attr, old, new):
try:
selections = new.indices
select_inds = [selections[0]]
if len(selections) == 1:
selected_issuer = source.data['y'][selections[0]]
for i in range(0, len(source.data['x'])):
if i != selections[0]:
issuer = source.data['y'][i]
if issuer == selected_issuer:
select_inds.append(i)
if len(selections) == 0:
for i in range(0, len(source.data['x'])):
select_inds.append(i)
new.indices = select_inds
except IndexError:
pass
pglyph.data_source.on_change('selected', callback)
curdoc().add_root(column(p))
请注意,您应该使用bokeh服务器来运行它。