Skip to content

When calling update_layout() on a figure, xaxis_range does not update correctly when margin_b is specified #5106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
goldrik opened this issue Mar 21, 2025 · 1 comment
Labels
bug something broken P1 needed for current cycle regression this used to work

Comments

@goldrik
Copy link

goldrik commented Mar 21, 2025

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.

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).

Image

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.

@gvwilson gvwilson added bug something broken P1 needed for current cycle regression this used to work labels Mar 24, 2025
@adeak
Copy link

adeak commented Apr 8, 2025

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P1 needed for current cycle regression this used to work
Projects
None yet
Development

No branches or pull requests

3 participants