I’m trying to create two linked plots where I can zoom in on either x-axis and have the corresponding y-axes automatically re-scale. As the data in the two plots is different, the range of y-axis values taken by each plot will be different and dependent upon the min and max of the data within the new selection.
I’ve tried many, many things but just can’t get it to work. A basic working setup of the problem is below:
from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import BoxZoomTool, ColumnDataSource, DataRange1d
import pandas as pd
import random
def gen_df(points):
df = pd.DataFrame()
df['x'] = range(0, points)
y1 = []
y2 = []
y1_now = 100
y2_now = 100
directions = ["UP", "DOWN", "HOLD"]
for i in df['x']:
step1 = random.choice(directions)
step2 = random.choice(directions)
if step1 == "UP":
y1_now += 1
elif step1 == "DOWN":
y1_now -= 1
if step2 == "UP":
y2_now += 1
elif step2 == "DOWN":
y2_now -= 1
y1.append(y1_now)
y2.append(y2_now)
df['y1'] = y1
df['y2'] = y2
return df
my_source = ColumnDataSource(data=gen_df(1000))
p1 = figure(width=1000, height=300, y_range=DataRange1d(only_visible=True), tools=[BoxZoomTool(dimensions='width'), 'reset'])
p1.step(source=my_source, x='x', y='y1', color='#A6CEE3', mode='after')
p2 = figure(width=1000, height=300, x_range=p1.x_range, y_range=DataRange1d(only_visible=True), tools=p1.tools)
p2.step(source=my_source, x='x', y='y2', color='#FB9A99', mode='after')
p2.x_range = p1.x_range
fig = gridplot([[p1], [p2]])
show(fig)
Bryan
September 14, 2022, 4:34pm
2
Windowed auto-ranging is still an open issue:
opened 10:36AM - 14 May 20 UTC
type: feature
### FOLLOW THESE INSTRUCTIONS CAREFULLY
*ISSUES THAT DO NOT CONTAIN NECESSARY… INFORMATION MAY BE CLOSED, IMMEDIATELY*
The issue tracker is NOT the place for general support. For questions and technical assistance, come join the [Bokeh Project Discourse](https://discourse.bokeh.org).
**Is your feature request related to a problem? Please describe.**
I have been working on implement an auto-ranging solution for my Bokeh based dash-board.
It is usually a good practice to have the plot nicely fit on the preferred axis (in my case it was the `y-axis`). In that way, the value ranges in the current window, which is a slice of the data after a drag and a zoom interaction, are efficiently fit and displayed proportionally. I believe this improves the usability, together with the readability of the plot.
**Describe the solution you'd like**
I would expect the values to be visually fit within their respective `min/max` ranges and displayed adaptively as we drag and zoom inside the Bokeh canvas.
**Describe alternatives you've considered**
While I was trying to implement this solution, I came across many "hacks" aimed at achieving this result. I think it would be a good idea to possibly provide a built-in, native approach for solving this problem.
**Additional context**
Right now, this is achieved though `js_on_change` callbacks that control the `y-range` or the `x-range` depending on your plot. Here is the result achieved after some callback hacking.

I have implemented the following hack:
[Python: Plot candlesticks with automatic Y zoom](https://stackoverflow.com/questions/51162010/python-plot-candlesticks-with-automatic-y-zoom
)
Besides the fact that many of these hacks are outdated, thus the constant need for fixing and adapting so they can work for the newer Bokeh versions, the overall experience introduces a lot of friction.
In my case, the reset of the `y-range` was fixed through the use of the `Range1d` range type. Below is a link to the Bokeh Community Support post.
[Dropdown Menu Event resetting Y-size on the second plot](https://discourse.bokeh.org/t/dropdown-menu-event-resetting-y-size-on-the-second-plot/5442/15)
I hope eventually there will be a more streamlined and standardised way to handle the "windowed auto-range".
For now you would have to manually compute and set new y-range bounds based on the data that is currently visible.
system
Closed
December 13, 2022, 4:34pm
3
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.