I would like to create a plot with 2 y-axes, whose ranges are being updated on a button click. The script would run on a Bokeh server. Note that in the code below, the primary y-axis is being updated by changing f.y_range.start/end. However, this is not possible with the secondary y-axis. I tried two other commands instead, i.e.
f.extra_y_ranges.update({"y2Range": Range1d(start=0, end=50)})
and
f.extra_y_ranges.update = {"y2Range": Range1d(start=0, end=50)}
But none of them work.
A similar questions was asked here: Bokeh: How to change extra axis visibility
# Import libraries
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, Range1d, LinearAxis
from bokeh.models.widgets import Button
from bokeh.layouts import layout
from bokeh.plotting import figure
# Create figure
f=figure()
# Create ColumnDataSource
source = ColumnDataSource(dict(x=range(0,100),y=range(0,100)))
# Create Line
f.line(x='x',y='y',source=source)
f.extra_y_ranges = {"y2Range": Range1d(start=0, end=100)}
f.add_layout(LinearAxis(y_range_name='y2Range'), 'left')
# Update axis function
def update_axis():
f.y_range.start = 0
f.y_range.end = 50
# Create Button
button = Button(label='Set Axis')
# Update axis range on click
button.on_click(update_axis)
# Add elements to curdoc
lay_out=layout([[f, button]])
curdoc().add_root(lay_out)
I was facing a similar problem. I was able to update the range of the secondary axis by accessing it through the 'extra_y_axis' dict via the name I created it with. For your case, it should look something like:
# Update primary axis function
def update_axis():
f.y_range.start = 0
f.y_range.end = 50
# Update secondary axis function
def update_secondary_axis():
f.extra_y_ranges['y2Range'].start = -20 #new secondary axis min
f.extra_y_ranges['y2Range'].end = 80 #new secondary axis max
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With