I want to plot a circle on a grid in python. I just need python to show the grid with squared cells. I wrote the following code, but it shows the grid with NON-squared cells.
Can anyone tell me how to make the grid cells be squared ?
import matplotlib.pyplot as plt
import math
p=8
R=0.484*p
t=np.linspace(0, 2*np.pi)
x=R*np.cos(t)
y=R*np.sin(t)
plt.axis("equal")
plt.grid(True, which='both', axis='both')
plt.plot(x,y)
plt.show()
Remove plt.axis("equal")
and instead set plt.gca().set_aspect('equal')
, which precisely sets the ratio of y-unit to x-unit of the axis scaling:
plt.grid(True, which='both', axis='both')
plt.plot(x,y)
plt.gca().set_aspect("equal")
plt.show()
Which would be the same as setting plt.axis('square')
.
Note that as mentioned in the docs, plt.axis("equal")
is equal to setting plt.gca().set_aspect('equal', adjustable='datalim')
, which will not produce the expected output, since data limits may not be respected in this case.
The above will give: