setting figure ranges using TapTool in bokeh

☆樱花仙子☆ 提交于 2019-12-10 21:45:08

问题


In the below example I have 2 scatter plots on top of each other. The intended behaviour is:

  • when clicking a point in the upper graph, the lower one zooms in to the surrounding region
  • when clicking a point in the lower graph, the lower plot zooms in to the surrounding region

I've got the first behaviour to run, but the second one doesn't seem to work: the x_range gets reset to cover the whole span of the data, ignoring the xrange.start= assignment in the callback.

# test_data_a is a pandas dataframe containing columns "x" and "y"
# test_data_b is a pandas dataframe containing columns "x" and "y"
f1=figure(width=950, tools="xwheel_zoom,box_zoom,reset,tap", height=200)
test_source1 = ColumnDataSource(data=dict(x=test_data_a.x, y=test_data_a.y))
test_source2 = ColumnDataSource(data=dict(x=test_data_b.x, z=test_data_b.z))
f1.circle("x", "y", fill_alpha=0.6, size=10, source=test_source1)


f2=figure(width=950, tools="reset,tap")
f2.circle("x", "z", fill_alpha=0.6, size=10, source=test_source2)

cb_click_testtop = CustomJS(args=dict(ts1=test_source1, ts2=test_source2, xrange=f2.x_range, yrange=f2.y_range), code="""
        index_selected=ts1.selected['1d'].indices[0]
        xmin=ts1.data['x'][index_selected]-0.5
        xmax=ts1.data['x'][index_selected]+0.5
        xrange.start=xmin
        xrange.end=xmax
""")

cb_click_testbot = CustomJS(args=dict(ts1=test_source1, ts2=test_source2, xrange=f2.x_range, yrange=f2.y_range), code="""
        index_selected=ts2.selected['1d'].indices[0]
        xmin=ts2.data['x'][index_selected]-0.5
        xmax=ts2.data['x'][index_selected]+0.5
        xrange.start=xmin
        xrange.end=xmax
""")
f1.add_tools(TapTool(callback=cb_click_testtop))
f2.add_tools(TapTool(callback=cb_click_testbot))


both= gridplot([[f1], [f2]])

show(both)

Another (simpler) example can be found here, where the same problem appears even when drawing only 1 plot.


回答1:


The default DataRange1d ranges only respond to user changes to start and end when they are set initially. Subsequently, a DataRange1d either respects the initial value, or always auto-ranges if one was not set. To make this kind of explicit control of ranges work, use a Range1d instead:

p=figure(x_range=(0,5)) 


来源:https://stackoverflow.com/questions/42163716/setting-figure-ranges-using-taptool-in-bokeh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!