Hi All,
in JupyterLab, using Plotly, I have set up a callback to update the y-axis when zooming.
Please refer to the code below for a complete example.
The zoom function is connected to the layout using on_change.
The yaxis
updates correctly when the figure is displayed using IPython’s display
or directly with fig
. However, it stops working if the figure.show()
method is used.
What is the issue with this method in this particular scenario? Why does on_change no longer work?
import plotly.graph_objs as go
import pandas as pd
import numpy as np
x = np.linspace(0, 100, 1000)
y = 0.1* x * np.sin(2 * np.pi * 0.1 * x)
# Create the DataFrame
df = pd.DataFrame({'Time': x, 'Value': y})
df = df.set_index('Time')
fig = go.FigureWidget()
fig.add_trace(
go.Scatter(x=df.index, y=df['Value'], mode='lines')
)
fig.update_layout(
xaxis={'rangeslider_visible': True}
)
# callback function to update yaxis min and max upon zoom
def zoom(layout, xrange):
in_view = df.loc[fig.layout.xaxis.range[0]:fig.layout.xaxis.range[1]]
fig.layout.yaxis.range = [in_view.Value.min(), in_view.Value.max()]
fig.layout.on_change(zoom, 'xaxis.range')
#fig # on_change (zoom) works!
display(fig) # on_change (zoom) works!
#fig.show() # on_change (zoom) doesn't work
I am aware that figure.show()
utilizes the renderers framework, but I am unable to understand its functionality in this “on_change” scenario.
Could someone shed some light on this or direct me to the appropriate documentation?
Thank you!
Please note: I posted this question to Stackoverflow also. Hope this’s ok!