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)
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.
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 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)