链接行为

链接绘图以在绘图之间添加连接的交互性通常很有用。本节介绍了一种简单的方法,使用|博克。绘图|接口。

链接平移

通常需要将平移或缩放操作链接到多个图中。要启用此功能,只需在 figure() 电话。

from bokeh.io import output_file, show
from bokeh.layouts import gridplot
from bokeh.plotting import figure

output_file("panning.html")

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

# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

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

# create a new plot and share only one range
s3 = figure(plot_width=250, plot_height=250, x_range=s1.x_range, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)

p = gridplot([[s1, s2, s3]], toolbar_location=None)

# show the results
show(p)

现在,您已经了解了如何使用|博克。绘图|接口。

连锁刷牙

Bokeh中的链接刷涂是通过在glyph渲染器之间共享数据源来表示的。这就是Bokeh需要理解的,对一个glyph操作的选择必须传递给共享同一源的所有其他glyph。若要查看链接选择如何扩展到只打印数据源中数据子集的glyph渲染器,请参见 带筛选数据的链接选择 .

下面的代码显示了两个不同的 figure() 电话。

from bokeh.io import output_file, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure

output_file("brushing.html")

x = list(range(-20, 21))
y0 = [abs(xx) for xx in x]
y1 = [xx**2 for xx in x]

# create a column data source for the plots to share
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))

TOOLS = "box_select,lasso_select,help"

# create a new plot and add a renderer
left = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
left.circle('x', 'y0', source=source)

# create another new plot and add a renderer
right = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
right.circle('x', 'y1', source=source)

p = gridplot([[left, right]])

show(p)

链接属性

也可以使用 js_link 方法。下面的示例将圆字形半径链接到滑块小部件的值:

from bokeh.layouts import column
from bokeh.models import Slider
from bokeh.plotting import figure, show

plot = figure(plot_width=400, plot_height=400)
r = plot.circle([1,2,3,4,5,], [3,2,5,6,4], radius=0.2, alpha=0.5)

slider = Slider(start=0.1, end=2, step=0.01, value=0.2)
slider.js_link('value', r.glyph, 'radius')

show(column(plot, slider))

链接是用JavaScript完成的,所以这个方法可以在独立的Bokeh文档中工作,或者在Bokeh服务器应用程序中工作。

添加小部件 有关不同窗口小部件的更多信息,以及 JavaScript回调 有关创建任意JavaScript回调的详细信息。