How to set the aspect ratio of the figure in Python Matplotlib.pyplot


The result is:


This page show how to set the aspect ratio of the figure in the python and matplotlib.pyplot. By adding set_aspect function, you can set the aspect ratio ot equal.
In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In [2]:
t = np.linspace(0,2*np.pi,100)
x = np.cos(t)
y = 0.1*np.sin(t) # 縦は-0.1から0.1まで
In [3]:
plt.figure(figsize=(3,3))
plt.plot(x,y)
Out[3]:
[<matplotlib.lines.Line2D at 0x1521a479a20>]
In [4]:
plt.figure(figsize=(3,3))
plt.plot(x,y)
plt.axes().set_aspect('equal', 'datalim')
plt.savefig("./aspect_ratio.png",dpi=250)