When calling update_layout() on a figure, xaxis_range does not update correctly when margin_b is specified
plotly 6.0.1
I create a figure like so
fig = go.Figure()
fig.add_trace(go.Scatter(x=np.arange(100), y=np.arange(100)))
fig.update_layout(xaxis_range=[50,None])
# fig.update_layout(margin_b=20)
fig.show()
Expected behavior
fig.update_layout(xaxis_range=[50,None]) (Line 3) should set the min xaxis value to be 50, while keeping the max value intact. It does this correctly (Correct: x-axis is from 50 to 100). Interestingly, when I click the modebar to download the plot as png, the xaxis range is incorrect in the output image.
Bug
If you uncomment the line fig.update_layout(margin_b=20) (Line 4) which adjusts the bottom margin, the x axis range is back to its original value unaffected by Line 3 (Inorrect: x-axis is from 0 to 100). Interestingly, if you double click the plot to reset the axes, the x axis changes to the updated value (50 to 100).
Note:
Weirdly, if I change Line 3 to fig.update_layout(xaxis_range=[50,100]) (specify 100 instead of None) and keep the margin_b line uncommented, the behavior is correct again.
I've noticed the same thing, but I'm starting to wonder if the margin is a red herring. Because when I omit the margin setting entirely, but change my browser window to a very small size, the set semi-automatic axes range gets overridden again. when I maximize the browser window (back to its initial size when the plot was rendered), the ranges are still wrong. If I press the "reset axes" button above the figure, the ranges snap back to the initial (correct) semi-automatic limits.
Another suspicious feature is that if I set "large" margins (e.g. margin_l=200) then the initial render of the figure is correct. When I set "small" margins (e.g. margin_l=5) then already the initial render violates the requested semi-automatic ranges.
Here's my minimal example that shows the "window unminimize/resizing leads to wrong range recalculation until axes are reset" issue (on my 1920x1200 resolution display in case the aspect ratio matters):
import plotly.graph_objects as go
x = list(range(-5, 6))
y = [x_now**2 for x_now in x]
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y))
fig.layout.xaxis.range = [-10, None]
# fig.layout.margin = {'l': 100, 'r': 50}
fig.show()
And I originally noticed the margin vs range interference on plotly 5.24 (5.24.1 specifically, I think).