散景,两个 y 轴,禁用一个轴进行缩放/平移

bokeh, two y axis, disable one axis for zoom/ panning

类似于 matplotlib ax.set_navigate(False) 命令可能吗?

这是一个使用 ipython 笔记本的最小示例。

from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d
from bokeh.io import output_notebook, show
output_notebook()

s1=figure(width=250, plot_height=250, title=None, tools="pan, wheel_zoom")
s1.line([1, 2, 3], [300, 300, 400], color="navy", alpha=0.5)
s1.extra_y_ranges = {"foo": Range1d(start=1, end=9)}
s1.add_layout(LinearAxis(y_range_name="foo"), 'right')
s1.line([1, 2, 3], [4, 4, 1], color="firebrick", alpha=0.5, y_range_name="foo")
show(s1)

是否可以固定第二个 y 轴,同时在另一个 y 轴上进行缩放和平移?
使用 PanTool 尺寸并没有帮助我解决这个问题。

编辑:插入截图:


蓝线画在第一个轴上,红线画在第二个

如果我放大,绕 x 轴平移,我希望红线保持原位。

您可以使用第二个 y 轴的 callback 功能来插入 JavaScript 代码,在执行缩放或平移时将范围重置为原始值:

from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d, CustomJS
from bokeh.io import output_notebook, show
output_notebook()

jscode="""
        range.set('start', parseInt(%s));
        range.set('end', parseInt(%s));
    """


s1=figure(width=250, plot_height=250, title=None, tools="pan, wheel_zoom")
s1.line([1, 2, 3], [300, 300, 400], color="navy", alpha=0.5)
s1.extra_y_ranges = {"foo": Range1d(start=1, end=9)}
s1.add_layout(LinearAxis(y_range_name="foo"), 'right')
s1.line([1, 2, 3], [4, 4, 1], color="firebrick", alpha=0.5, y_range_name="foo")


s1.extra_y_ranges['foo'].callback = CustomJS(
        args=dict(range=s1.extra_y_ranges['foo']),
                    code=jscode % (s1.extra_y_ranges['foo'].start,
                                   s1.extra_y_ranges['foo'].end)
            )

show(s1)

Jakes 的回答在 bokeh 1.3.4 中不再适用于我。但是现在可以简单地使用 Range1d.bounds 属性来完成:

from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d
from bokeh.io import show

s1 = figure(width=250, plot_height=250, title=None, tools="pan, wheel_zoom")
s1.line([1, 2, 3], [300, 300, 400], color="navy", alpha=0.5)
s1.extra_y_ranges = {"foo": Range1d(start=1, end=9, bounds=(1,9))}
s1.add_layout(LinearAxis(y_range_name="foo"), 'right')
s1.line([1, 2, 3], [4, 4, 1], color="firebrick", alpha=0.5, y_range_name="foo")

show(s1)