Keep ranges after changing glyph source

Hi,

how do I keep the ranges after changing the source of a glyph?
The following code should keep the old range, but the range does change:

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Button
from bokeh.plotting import figure, curdoc, show

figure = figure()
source = ColumnDataSource(data={"x": [0, 1, 0, 1], "y": [0, 0, 1, 1]})
figure.patch(x="x", y="y", source=source)
button = Button(label="bigger")

def bigger(event):
    source.data["x"] = [0, 2, 0, 2]
    figure.x_range.end = 1

button.on_click(bigger)

curdoc().add_root(column(figure))

I think setting the range happens before the rerendering of the glyph.
Is there a general way, to set a glyph to not effect any ranges, or a way to set the range manually after updating a source.

By default DataRange1d models are assigned to figure’s x_range and y_range, meaning that the axes bounds will follow the data that is plotted on the figure, so when you change the source data, the axes bounds are recalculated. If you want “static” axes bounds, or axes bounds that you explicitly set via callback, you want to assign a Range1d model:

from bokeh.plotting import figure
from bokeh.models import Range1d

f = figure()
f.x_range = Range1d(start=0, end=10)

You can then alter the start/end properties of this in your callbacks etc if you want, or leave it alone to hold the axes bounds static regardless of the data being plotted on it.

See ranges — Bokeh 2.4.3 Documentation

2 Likes