Preserve zoom after plot update

Is it possible to preserve the zoomed axes after a plot is updated?

For example, I have a plot with vertical spans highlighting features, if I zoom in on a single span then update the plot the zoom resets to show the entire plot.

The code below will generate a plot with vertical spans and a button that can add/remove them.

import numpy as np
from bokeh.layouts import row, widgetbox
from bokeh.models import Toggle, Span
from bokeh.plotting import curdoc, figure

def toggle_button(state):
    layout.children[1] = draw_plot()

def draw_plot():
    p = figure(
        toolbar_location="above",
        tools=['xbox_zoom', 'save', 'reset'],
    )
    p.line(x, y, line_width=1)
    if w['toggle'].active:
        for label in l:
            line = Span(
                location=label,
                dimension='height',
                line_color='green',
                line_dash='dashed',
                line_width=1
            )
            p.add_layout(line)
    return p

x = np.arange(0, 1000, 1)
y = np.random.randint(low=200, high=600, size=1000)
l = [200, 400, 555, 888]

w = dict()
w['toggle'] = Toggle(
    label="Toggle",
    active=True
    )
w['toggle'].on_click(toggle_button)

p = draw_plot()

wb = widgetbox(list(w.values()))
layout = row(wb, p)
curdoc().add_root(layout)

Hi,

The default range for plots is a DataRange1d, which auto-ranges to fit the available data. If you re updating the data, that is probably what is causing the range to snap to the new data bounds. I think if you don't want this auto-ranging behavior, you will need to set an explicit range in figure, e.g.:

  p = figure(..., x_range=(initial_start, initial_end))

Now any range changes will have to be made explicitly, either via an interactive tool, or code that sets p.x_range.start or p.x_range.end, etc.

Thanks,

Bryan

ยทยทยท

On Feb 15, 2018, at 20:51, [email protected] wrote:

Is it possible to preserve the zoomed axes after a plot is updated?

For example, I have a plot with vertical spans highlighting features, if I zoom in on a single span then update the plot the zoom resets to show the entire plot.

The code below will generate a plot with vertical spans and a button that can add/remove them.

import numpy as np
from bokeh.layouts import row, widgetbox
from bokeh.models import Toggle, Span
from bokeh.plotting import curdoc, figure

def toggle_button(state):
    layout.children[1] = draw_plot()

def draw_plot():
    p = figure(
        toolbar_location="above",
        tools=['xbox_zoom', 'save', 'reset'],
    )
    p.line(x, y, line_width=1)
    if w['toggle'].active:
        for label in l:
            line = Span(
                location=label,
                dimension='height',
                line_color='green',
                line_dash='dashed',
                line_width=1
            )
            p.add_layout(line)
    return p

x = np.arange(0, 1000, 1)
y = np.random.randint(low=200, high=600, size=1000)
l = [200, 400, 555, 888]

w = dict()
w['toggle'] = Toggle(
    label="Toggle",
    active=True
    )
w['toggle'].on_click(toggle_button)

p = draw_plot()

wb = widgetbox(list(w.values()))
layout = row(wb, p)
curdoc().add_root(layout)

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/856f5493-01e3-46df-a8ca-93f18bc436d3%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.