Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

bokeh

similar to the matplotlib ax.set_navigate(False) command possible?

Here is a minimal example using ipython notebook.

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)

Is is possible to hold the second y axis in place, while zooming and panning in the other y axis?
Using PanTool dimensions did not help me with this problem.

Edit: Screenshot inserted:

Screenshot actual output
Blue line is drawn on the first axis, red on the second

If I zoom in, pan around the x axis, I want the red line to keep in place.

like image 916
cheesepalm Avatar asked Dec 04 '15 09:12

cheesepalm


1 Answers

You can use the callback functionality of the second y axis to insert JavaScript code that resets the range to the original values whenever a zoom or pan is executed:

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)
like image 168
Jake Avatar answered Sep 19 '22 21:09

Jake