Bokeh:在多个图形上使用相同的工具效果

3
我在bokeh图中的一列中有多个图形。我想同时应用相同的工具转换,即,如果我放大一个图形,则所有图形都应该放大;如果我平移一个图形,则它们都应该平移;如果我重置一个图形,它们都应该重置(我不是很关心悬停,只要有放大、平移和重置就可以了)。
是否有bokeh方法来链接这些图形,或者我需要一些自定义JavaScript代码来实现(如果需要,那么该怎么做)?
谢谢您提前的回答。
编辑:
感谢@bigreddot和@Abhinav提供的解决方案。您需要根据此处的描述使用他们的答案:Linking Plots。范围促进了平移,而相同的数据源促进了缩放。
从layouts示例中修改的解决方案:
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from  bokeh.models import PanTool,ResetTool,BoxZoomTool


output_file("layout.html")

x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]


tools=[BoxZoomTool(), PanTool(), ResetTool()]

datasource = ColumnDataSource({'x': x, 'y0': y0, 'y1': y1, 'y2': y2})
# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None,tools=tools)
s1.circle('x', 'y0', size=10, color="navy", alpha=0.5, source=datasource)

# create another one
s2 = figure(plot_width=250, plot_height=250, title=None,tools=tools,x_range=s1.x_range,y_range=s1.y_range)
s2.triangle('x', 'y1', size=10, color="firebrick", alpha=0.5, source=datasource)

# create and another
s3 = figure(plot_width=250, plot_height=250, title=None,tools=tools,x_range=s1.x_range,y_range=s1.y_range)
s3.square('x', 'y2', size=10, color="olive", alpha=0.5, source=datasource)

# put the results in a column and show
show(column(s1, s2, s3))

2
我已经回滚了您将问题转化为解决方案的操作:请在修订历史记录中找到您的解决方案,并将其作为单独的答案发布,谢谢。 - Cœur
2个回答

4
这是文档中的内容,在链接图表下。
如果想要图表共享范围,则需共享真正的范围对象。
# create a new plot
s1 = figure()
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

# create a new plot and share both ranges
s2 = figure(x_range=s1.x_range, y_range=s1.y_range)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)

1
你应该从数据中创建一个ColumnDataSource对象,并在创建图形时使用它。只要这些图形共享相同的数据源,它们就会具有相同的工具效果。当使用ColumnDataSource时,你需要使用数据源中的列名称来指定数据,而不是直接传递数组本身。例如:
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from  bokeh.models import PanTool,ResetTool,HoverTool,WheelZoomTool,SaveTool,BoxZoomTool


output_file("layout.html")

x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]


#tools='hover,xpan,xwheel_zoom,box_zoom,save,reset'
tools=[HoverTool(),BoxZoomTool(dimensions='width'), PanTool(dimensions='width'),  SaveTool(), ResetTool()]

datasource = ColumnDataSource({'x': x, 'y0': y0, 'y1': y1, 'y2': y2})
# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None,tools=tools)
s1.circle('x', 'y0', size=10, color="navy", alpha=0.5, source=datasource)

# create another one
s2 = figure(plot_width=250, plot_height=250, title=None,tools=tools)
s2.triangle('x', 'y1', size=10, color="firebrick", alpha=0.5, source=datasource)

# create and another
s3 = figure(plot_width=250, plot_height=250, title=None,tools=tools)
s3.square('x', 'y2', size=10, color="olive", alpha=0.5, source=datasource)

# put the results in a column and show
show(column(s1, s2, s3))

这适用于选择缩放范围,但不适用于实际缩放或平移。 - azazelspeaks
1
啊,对了。还需要设置x_range和y_range,我忘了这一点。 - Abhinav Upadhyay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接