How to equalize the scales of x-axis and y-axis in matplotlib

PythonMatplotlib

Python Problem Overview


I wish to draw lines on a square graph.

The scales of x-axis and y-axis should be the same.

e.g. x ranges from 0 to 10 and it is 10cm on the screen. y has to also range from 0 to 10 and has to be also 10 cm.

The square shape has to be maintained, even if I mess around with the window size.

Currently, my graph scales together with the window size.

How may I achieve this?

UPDATE:

I tried the following, but it did not work.

plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.axis('equal')

Python Solutions


Solution 1 - Python

You need to dig a bit deeper into the api to do this:

from matplotlib import pyplot as plt
plt.plot(range(5))
plt.xlim(-3, 3)
plt.ylim(-3, 3)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()

doc for set_aspect

Solution 2 - Python

plt.axis('scaled')

works well for me.

Solution 3 - Python

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.

Solution 4 - Python

Try something like:

import pylab as p
p.plot(x,y)
p.axis('equal')
p.show()

Solution 5 - Python

you can stretch the plot to square using this :

fig = plt.figure(figsize=(1, 1))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSibbs GamblingView Question on Stackoverflow
Solution 1 - PythontacaswellView Answer on Stackoverflow
Solution 2 - PythonmyxView Answer on Stackoverflow
Solution 3 - PythonAdam StewartView Answer on Stackoverflow
Solution 4 - PythonDman2View Answer on Stackoverflow
Solution 5 - PythonrzbView Answer on Stackoverflow