1

I would like to change the visible range of the y-axis each time when the x-axis range changes. But something does not seem to work, panning and changing the x-axis range does not seem to invoke the callback or something is wrong with the callback?

x = list(range(100))
y = list(np.random.randint(-10, 10, 100))
y = np.cumsum(y)
p1 = figure(title="Random", width=600, height=600)
p1.line(x, y, color="red")

callback = CustomJS(args=dict(yrange=p1.y_range), code="""
           yrange.start=-10;
           yrange.end=10;""")
                
p1.x_range.js_on_change("end", callback)

show(p1)
Matt
  • 7,004
  • 11
  • 71
  • 117
  • I think I misunderstood it, but if you choose the same value for the `100` in `x`, and the last value in definition of `y`, x-axis is changing (both indeed). – Memristor May 03 '23 at 19:41
  • @Memristor, sorry, I edited the code, I want the y-axis range to reset to between -10 and 10 whenever the x-axis range changes. But nothing is happening... – Matt May 03 '23 at 19:48
  • I have managed to cap the y-axis using `p1.y_range = Range1d(-10, 10, bounds="auto")` at the end of the code (before, `show`), not sure that is what you want. `Range1d` is on `bokeh.models`. – Memristor May 03 '23 at 20:26
  • No, I am trying to get the callback to work. Ultimately, I want to set the y-axis range to the min and max of the actual data between the x-Axis min and max. This requires me to be able to set the y-axis range **inside** the callback. – Matt May 03 '23 at 20:43
  • I think you need to put your data in a `ColumnDataSource` class, then feed this to the `CustomJS` dict to achieve this. – Memristor May 03 '23 at 21:56

1 Answers1

1

Auto-ranging to show the appropriate range of y-values depending on the displayed portion of x-axis has been implemented for the HoloViews Bokeh backend here.

While this isn't a simple, pure Bokeh example, this does show that the behavior you want is possible using CustomJS (this line in particular seems relevant). Hope that is helpful!

jlstevens
  • 201
  • 1
  • 2