In order to automatically rescale the y-axis of a Bokeh plot based on the data in the plot, you can use a JavaScript callback function defined by CustomJS. This function takes two arguments: args, a dictionary of objects to make available to the JavaScript code, and code, the JavaScript code that will be executed when the callback is triggered.
In the code of the callback function, you can calculate the minimum and maximum values of the low and high data columns of your data source within the current x-range of the plot. Then, you can set the start and end values of the y_range object to be a percentage padding around the minimum and maximum values.
Finally, you can use the js_on_change method on the x_range object of the plot to add the callback function as a JavaScript callback that will be triggered when the start and end values of the x_range object change.
Overall, this solution provides a way to automatically rescale the y-axis of a Bokeh plot based on the data in the plot, which can be useful for visualizing data that may change dynamically over time
# JavaScript callback function to automatically zoom the Y axis to
callback = CustomJS(args={'y_range': main.y_range, 'source': source}, code='''
clearTimeout(window._autoscale_timeout);
var Index = source.data.time, Low = source.data.low, High = source.data.high, start = cb_obj.start, end = cb_obj.end, min = Infinity, max = -Infinity;
for (var i=0; i < Index.length; ++i) {
if (start <= Index[i] && Index[i] <= end) {
max = Math.max(High[i], max); min = Math.min(Low[i], min);
}
}
var pad = (max - min) * .09;
window._autoscale_timeout = setTimeout(function() {
y_range.start = min - pad; y_range.end = max + pad;
});
'''.replace('\n', '').replace('\t','')
)
main.x_range.js_on_change('start', callback)
main.x_range.js_on_change('end', callback)
