How do I equalize the scales of the x-axis and y-axis?
Asked Answered
D

4

225

How do I create a plot where the scales of x-axis and y-axis are the same?

This equal ratio should be maintained even if I change the window size. Currently, my graph scales together with the window size.

I tried:

plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.axis('equal')
Democracy answered 1/8, 2013 at 9:58 Comment(1)
for 3d, you have to do a little bit extra work: #13685886Chiba
M
308

Use Axes.set_aspect in the following manner:

from matplotlib import pyplot as plt
plt.plot(range(5))
plt.xlim(-3, 3)
plt.ylim(-3, 3)
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.draw()
Modify answered 1/8, 2013 at 13:56 Comment(12)
Awesome! It indeed works like charm. Could you please tell me what plt.plot(range(5)) and plt.gca().set_aspect('equal', adjustable='box') do, if you don't mind? Also, I notice that even if I don't have plt.draw(), the plot will still show up. Then what is the use of it?Democracy
the plot is just have something to show. For the set_aspect read the documentation link. The draw is just to make sure it gets rendered.Modify
Sorry but I am asking about the range(5) thing inside plot(). What does the range(5) do?Democracy
ah, range(5) returns [0, 1, 2, 3, 4] See: docs.python.org/2/library/functions.html#rangeModify
yah, I know it returns that, but then why do I feed the list to plot()?Democracy
to have some fake data to plot (it should have plotted a straight line).Modify
@perfectionm1ng no worries, took me a while to figure out what you were asking.Modify
I don't see the need to set both limits to the same ranges, this can result in different scales, especially when most plots are not square shaped.Goulder
Is there a way to get this done without having to specify exactly the limits? I would have expected there to be a simple command to get a square plot with the same scale and ticks for both axis. ThanksBillionaire
@Confounded: As stated below, if you don't want to give any limits you can just use plt.axis('square')Demonize
This is really great, thank you. For a specific subplot you can also do ax.set_aspect('equal', adjustable='box')Parental
how in the name of all that is good and true is this not the default ? I tremble to wonder how many bad decisions have been made as a result of looking at data with incorrectly scaled axes...Harbert
E
105
plt.axis('scaled')

works well for me.

Ethos answered 17/12, 2016 at 16:15 Comment(4)
Also worked for me. Just make sure to use this before setting limits/ticks, as it will rescale automatically.Stalk
Sorry, plt.axis('scaled') didn't work for me in Python 3.7 and matplotlib - matplotlib==3.1.0 However, plt.axis('square') worked!Kimbrough
@rishijain ValueError: Unrecognized string squared to axis; try on or offOsmium
@MonaJalal it's plt.axis('square') not plt.axis('squared')Delorenzo
H
73

See the documentation on plt.axis(). This:

plt.axis('equal')

doesn't work because it changes the limits of the axis to make circles appear circular. What you want is:

plt.axis('square')

This creates a square plot with equal axes.

Hyacinth answered 1/9, 2019 at 18:55 Comment(0)
D
24

Try something like:

import pylab as p
p.plot(x,y)
p.axis('equal')
p.show()
Dealing answered 1/8, 2013 at 10:55 Comment(7)
This works on my system, perhaps you could show a portion of the code you are working on? Might be able to help work out the issue quicker.Dealing
This does NOT work in general. The axes are equal, but the plot is not square, unless the plotting window is also square. Tested with Matplotlib 2.0Phenyl
P.axis('equal') seems to be like P.gca().set_aspect('equal', adjustable='datalim'). While if adjustable='box', then the plot becomes square.Metathesize
I definitely do not get a square box out of this.Purpurin
pylab is deprecatedWeir
If you want to also have the grid square, in addition to aspect as mentioned in the other answers, see https://mcmap.net/q/120267/-square-major-minor-grid-for-axes-with-different-limitsGlaciology
@PeterDrake: The goal isn't to get a square box, but to have the same scale in the x and y axes. So circles will be displayed as circles, but if you don't plot a square or any object with the same width as the height, you won't get a square box either.Mannuela

© 2022 - 2024 — McMap. All rights reserved.