Issue
I have the problem that somehow, if I use predict-idlab's awesome plotly_resampler package for plotting large datasets with plotly, I can't properly scale the y-axis of all subplots in a figure. Consider the following code
from plotly_resampler import register_plotly_resampler, unregister_plotly_resampler
from plotly.subplots import make_subplots
import plotly.graph_objects as go
register_plotly_resampler(mode="auto", default_n_shown_samples=10000)
channels = [([1,2,3],[-10, 0, 10]), ([1,2,3], [5,4,0])]
fig = make_subplots(rows=len(channels), cols=1, subplot_titles=['subplot1', 'subplot2'], shared_xaxes=True)
for j, channel in enumerate(channels):
x = channel[0]
y = channel[1]
fig.add_trace(go.Scattergl(x=x, y=y), row=j+1, col=1)
fig.update_xaxes(title_text='x', row=j+1, col=1)
fig.update_yaxes(title_text='y', range=[-1.25, 1.25], row=j+1, col=1)
fig.update_layout(title_text='Plot Title')
display(fig)
unregister_plotly_resampler()
This results in the following plot:
So as can be seen the y-axis of the first subfigure is not scaled to the desired range. However, if I remove the register/unregister_plotly_resampler()
lines, everything works like a charm:
What's the matter here?
EDIT=========================
I found a workaround:
If the y-axis is formatted with fig.update_yaxes(range = [-1.5,1.5])
or fig.update(layout_yaxis_range = [-1.5,1.5])
before running register_plotly_resampler()
, everything works fine.
Solution
This seems to be a known bug. The proposed workaround for the moment is to set autorange to False: fig.update_xaxes(autorange=False)
. The following code snippet gives the desired result:
from plotly_resampler import register_plotly_resampler, unregister_plotly_resampler
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# plot with plotly: with FigureResampler
register_plotly_resampler(mode="auto", default_n_shown_samples=10000)
channels = [([1,2,3],[-10, 0, 10]), ([1,2,3], [5,4,0])]
fig = make_subplots(rows=len(channels), cols=1, subplot_titles=['subplot1', 'subplot2'], shared_xaxes=True)
for j, channel in enumerate(channels):
x = channel[0]
y = channel[1]
fig.add_trace(go.Scattergl(x=x, y=y), row=j+1, col=1)
fig.update_xaxes(title_text='x', row=j+1, col=1)
fig.update_yaxes(title_text='y', range=[-1.25, 1.25], row=j+1, col=1)
fig.update_layout(title_text='Plot Title')
fig.update(layout_yaxis_range = [-1,1])
fig.update_yaxes(range = [-1.25,1.25], autorange=False)
display(fig)
unregister_plotly_resampler()
Answered By - ilja
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.