Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib.pyplot, preserve aspect ratio of the plot

Assuming we have a polygon coordinates as polygon = [(x1, y1), (x2, y2), ...], the following code displays the polygon:

import matplotlib.pyplot as plt plt.fill(*zip(*polygon)) plt.show() 

By default it is trying to adjust the aspect ratio so that the polygon (or whatever other diagram) fits inside the window, and automatically changing it so that it fits even after resizing. Which is great in many cases, except when you are trying to estimate visually if the image is distorted. How to fix the aspect ratio to be strictly 1:1?

(Not sure if "aspect ratio" is the right term here, so in case it is not - I need both X and Y axes to have 1:1 scale, so that (0, 1) on both X and Y takes an exact same amount of screen space. And I need to keep it 1:1 no matter how I resize the window.)

like image 963
Headcrab Avatar asked May 29 '10 11:05

Headcrab


People also ask

How do you set aspect ratio on a plot?

The plot box aspect ratio is the relative lengths of the x-axis, y-axis, and z-axis. By default, the plot box aspect ratio is based on the size of the figure. You can change the aspect ratio using the pbaspect function. Set the ratio as a three-element vector of positive values that represent the relative axis lengths.

What is matplotlib aspect ratio?

The aspect ratio of a matplotlib plot refers to the aspect of the axis scaling, i.e. the ratio of y-unit to x-unit. This ratio can be modified by using the matplotlib. axes. Axes. set_aspect() function.


1 Answers

Does it help to use:

plt.axis('equal') 
like image 162
Olivier Verdier Avatar answered Sep 28 '22 16:09

Olivier Verdier