41

I am using plotly for python and I can't set x and y axis so they could have the same scale:

Here is my layout:

layout = Layout(
    xaxis=XAxis(
        range=[-150, 150],
        showgrid=True,
        zeroline=True,
        showline=True,
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    ),
    yaxis=YAxis(
        range=[-150,150],
        showgrid=True,
        zeroline=True,
        showline=True,
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    )
)

And then I get something like this!

enter image description here

Why is the scale is different for x and y? that will affect my visualization.

How can I get a grid with a square cells?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
farhawa
  • 10,120
  • 16
  • 49
  • 91

4 Answers4

78

Finally, this feature is implemented.

layout = go.Layout(yaxis=dict(scaleanchor="x", scaleratio=1))

Update: in new versions of plotly, use the following:

fig.update_yaxes(
    scaleanchor="x",
    scaleratio=1,
  )

See example here https://plot.ly/python/axes/#fixed-ratio-axes.

Robin De Schepper
  • 4,942
  • 4
  • 35
  • 56
Max
  • 1,685
  • 16
  • 21
  • 7
    Is there a way to do this for 3D plots? I've tried `go.Layout(zaxis=dict(scaleanchor="x", scaleratio=1))` but that didn't work. – Ray Apr 20 '18 at 11:57
  • 4
    @Ray See this: https://community.plot.ly/t/creating-a-3d-scatterplot-with-equal-scale-along-all-axes/15108, and this: https://plot.ly/python/3d-axes/ – ap21 Feb 18 '19 at 11:40
  • However, this also limits the zoom interactive tool being only square. – nn0p Mar 04 '20 at 12:14
  • 1
    It looks like it's updated: `fig.update_yaxes(scaleanchor = "x", scaleratio = 1)` – Gunther Struyf Mar 16 '21 at 12:35
9

You can assign same length for height and width in your layout. Here is an example:

layout = Layout(
    xaxis=XAxis(
       range=[-150, 150],
       showgrid=True,
       zeroline=True,
       showline=True,
       gridcolor='#bdbdbd',
       gridwidth=2,
       zerolinecolor='#969696',
       zerolinewidth=4,
       linecolor='#636363',
       linewidth=6
    ),
    yaxis=YAxis(
        range=[-150,150],
        showgrid=True,
        zeroline=True,
        showline=True,
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
   ),
   height=600,
   width=600,
)
neda
  • 909
  • 7
  • 4
  • 5
    Note that this doesn't work if there is a legend, since the legend also takes up some of those 600 pixels. @Brut's answer is more robust. – waterproof Jun 06 '18 at 19:43
2

@neda's answer only works for equal ranges on both x and y - which is seldom the case. This seems to be something a lot of people are asking for, something like matplotlib's axis('equal'). See https://github.com/plotly/plotly.py/issues/70

For now, I use a multiplier to both ranges separately - essentially defining how long each unit length is on each axis.

height=(yMax - yMin) * mul
width= (xMax - xMin) * mul

Even by doing this, the grid is not a %100 perfect square..

ahmedhosny
  • 1,099
  • 14
  • 25
0
ml_mx = max(Curve)
ml_mn = min(Curve)
ap_mx = max(Curve2)
ap_mn = min(Curve2)

if ml_mx > ap_mx:
    mx = ml_mx
else:
    mx = ap_mx

if ml_mn < ap_mn:
    mn = ml_mn
else:
    mn = ap_mn

mx = mx + mx * 0.2
mn = mn - mn * 0.2

fig_graficas.update_yaxes(
    range = [mn,mx]
)
fig_graficas.update_xaxes(
    range = [mn,mx]
)
  • 1
    While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run. – Tomer Shetah Oct 03 '20 at 22:37