matplotlib.pyplot, preserve aspect ratio of the plot

Solution 1:

Does it help to use:

plt.axis('equal')

Solution 2:

'scaled' using plt

The best thing is to use:

 plt.axis('scaled')

As Saullo Castro said. Because with equal you can't change one axis limit without changing the other so if you want to fit all non-squared figures you will have a lot of white space.

Equal

enter image description here

Scaled

enter image description here

'equal' using ax

Alternatively, you can use the axes class.

fig = plt.figure()
ax = figure.add_subplot(111)
ax.imshow(image)
ax.axes.set_aspect('equal')

Solution 3:

There is, I'm sure, a way to set this directly as part of your plot command, but I don't remember the trick. To do it after the fact you can use the current axis and set it's aspect ratio with "set_aspect('equal')". In your example:

import matplotlib.pyplot as plt
plt.fill(*zip(*polygon))
plt.axes().set_aspect('equal', 'datalim')
plt.show()

I use this all the time and it's from the examples on the matplotlib website.