Tried to fix axes range of a plot but got expanded margins

What I did:

fig = make_subplots(
    rows=2, cols=2, 
)

fig.add_trace(
    go.Scatter(x=fpr, y=tpr, mode='lines', name='Receiver Operating Characteristic', line=go.scatter.Line(color="blue")),
    row=1, col=1
)
fig.add_trace(
    go.Scatter(x=[0, 1], y=[0, 1], mode='lines'),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=thresholds, y=tpr, mode='lines', name='tpr', line=go.scatter.Line(color="blue")),
    row=1, col=2
)
fig.add_trace(
    go.Scatter(x=thresholds, y=fpr, mode='lines', name='fpr', line=go.scatter.Line(color="red")),
    row=1, col=2
)

fig.update_xaxes(
    range=[0,1],
    dtick=0.2,
    autorange=False,
)

fig.update_yaxes(
    range=[0,1],
    dtick=0.2,
    autorange=False,
    scaleanchor='x',
    scaleratio=1,
)

fig.update_layout(
    width=800,
    height=800,
    margin={'t': 0, 't': 0, 'r': 0, 'l': 0, },
)

fig.show()

Essentially the axes ranges on the canvas were intended to be set exactly within [0, 1], however It actually seemed to put an extra strip at both the top and bottom of the plot, as illustrated in the attachment. Is there any way to get a plot where both axes show the exact ranges as specified?
20191230151949

Hi @jingw222, you need to add constrain='domain' when setting the axis configuration, as in the figure below. I’ve made a note in https://github.com/plotly/plotly.py/issues/1965 to add such an example to the documentation (of course if you would like to do it yourself, that’d be awesome!).

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np
fig = make_subplots(
    rows=1, cols=2, 
)

fig.add_trace(
    go.Scatter(x=np.random.random(10), y=np.random.random(10)),
    row=1, col=1
)
fig.add_trace(
    go.Scatter(x=np.random.random(10), y=np.random.random(10)),
    row=1, col=2
)
        
fig.update_xaxes(
    range=[0,1],
    dtick=0.2,
    constrain='domain'
)

fig.update_yaxes(
    range=[0,1],
    dtick=0.2,
    scaleanchor='x',
    scaleratio=1,
    constrain='domain'
)

fig.update_layout(
    width=800,
    height=400,
    margin={'t': 0, 't': 0, 'r': 0, 'l': 0, },
)

fig.show()

Thanks a lot. Just submitted a PR to this issue. And happy new year. :smile:

1 Like

Thank you very much for your contribution to the library :tada: ! Happy New Year and happy coding :wink:

1 Like