在一个web应用程序中,我想让用户使用bokeh的优美框/套索选择工具选择绘制图像中感兴趣的区域。然后,我希望在python中接收选定的像素以进行进一步操作。
对于散点图来说,这与示例画廊类似,很容易实现。
然而,如果我用其他图表替换散点图,则需要进行以下步骤。
对于散点图来说,这与示例画廊类似,很容易实现。
import bokeh.plotting
import numpy as np
# data
X = np.linspace(0, 10, 20)
def f(x): return np.random.random(len(x))
# plot and add to document
fig = bokeh.plotting.figure(x_range=(0, 10), y_range=(0, 10),
tools="pan,wheel_zoom,box_select,lasso_select,reset")
plot = fig.scatter(X, f(X))
#plot = fig.image([np.random.random((10,10))*255], dw=[10], dh=[10])
bokeh.plotting.curdoc().add_root(fig)
# callback
def callback(attr, old, new):
# easily access selected points:
print sorted(new['1d']['indices'])
print sorted(plot.data_source.selected['1d']['indices'])
plot.data_source.data = {'x':X, 'y':f(X)}
plot.data_source.on_change('selected', callback)
然而,如果我用其他图表替换散点图,则需要进行以下步骤。
plot = fig.image([np.random.random((10,10))*255], dw=[10], dh=[10])
使用图像上的选择工具不会改变plot.data_source.selected
中的任何内容。
我相信这是预期的行为(也很合理),但如果我想选择图像的像素怎么办?当然,我可以在图像上放置一个不可见散点网格,但是否有更优雅的方法来实现这一点呢?