python - 在 Bokeh 中使用 TapTool 设置图形范围

标签 python bokeh

在下面的例子中,我有 2 个散点图在彼此之上。预期行为是:

  • 当点击上图中的一个点时,下图会放大到周围区域
  • 当点击下图中的一个点时,下图会放大到周围区域

我有第一个行为要运行,但第二个似乎不起作用:x_range 被重置以覆盖整个数据范围,忽略 xrange .start= 回调中的赋值。

# 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)

可以找到另一个(更简单的)例子here ,即使只绘制 1 个图也会出现同样的问题。

最佳答案

默认的 DataRange1d 范围仅在 初始 设置时响应用户对 startend 的更改。随后,DataRange1d 要么遵循初始值,要么始终自动调整范围(如果未设置)。要使这种范围的显式控制起作用,请改用 Range1d:

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

关于python - 在 Bokeh 中使用 TapTool 设置图形范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42163716/

相关文章:

python - 如何一次从数组中删除多个值

python - 类型错误 - Python Simpy

python - 在python中编辑列表中的元素

python - 访问全息 View 中的 Bokeh (图形)参数

python - Bokeh 弃用警告 : Setting a fixed font size value as a string 'text_font_size' is deprecated

python - mongoengine : ReferenceFields will default to using ObjectId 中的问题

python - 如何计算 numpy 二维网格中的孔数?

Python Bokeh : Plotting same chart multiple times in gridplot

javascript - 如何使用外部 JavaScript 代码访问和更新 Bokeh 图或小部件?

python - 写下 Bokeh 图所选数据不起作用