0
votes

I have made a code (in python) that makes a point on the circumference of a circle at every 10 degrees:

rad = 75
originX = originY = 0
tenDegreePts = [[],[]]

for theta in range(0, 360, 10):
    b = (np.cos(theta))*rad
    a = (np.sin(theta))*rad
    tenDegreePts[0].append(originX+b)
    tenDegreePts[1].append(originY+a)

plt.plot(tenDegreePts[0],tenDegreePts[1],'o')
plt.ylim(-100,100)
plt.xlim(-100,100)

The program runs perfectly, but for some reason the circle has more of an elliptical shape. Also, the points on the graph aren't equal distances apart:

Scatter plot of points on the circumference of a circle at every 10 degrees:
scatter plot of the points on the circle

(In the picture you can't see the axis but they go from -100 to 100 on both x and y)

2

2 Answers

4
votes

You can use plt.axis('equal') to maintain the correct aspect ratio of the plot.

About the problem of the points not being evenly spaced: you give theta in degrees, but the trigonomial functions expect their inputs in radians. You can convert degrees to radians multiplying by 180 and dividing by Pi, as in the code.

I also rewrote the code a bit to make better use of numpy's broadcasting tricks.

import matplotlib.pyplot as plt
import numpy as np

originX = originY = 0
theta = np.linspace(0, 360, 37)

for rad in range(15, 85, 10):
    tenDegreePtsX = np.cos(theta*np.pi/180) * rad + originX
    tenDegreePtsY = np.sin(theta*np.pi/180) * rad + originY
    plt.plot(tenDegreePtsX, tenDegreePtsY, 'o', ms=rad/10)

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

plt.show()

corrected plot

2
votes

Check https://matplotlib.org/gallery/subplots_axes_and_figures/axis_equal_demo.html Which shows exactly this.

Copying it here for the people who are not familiar with how to read the docs.

import matplotlib.pyplot as plt
import numpy as np

# Plot circle of radius 3.

an = np.linspace(0, 2 * np.pi, 100)
fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(3 * np.cos(an), 3 * np.sin(an))
axs[0, 0].set_title('not equal, looks like ellipse', fontsize=10)

axs[0, 1].plot(3 * np.cos(an), 3 * np.sin(an))
axs[0, 1].axis('equal')
axs[0, 1].set_title('equal, looks like circle', fontsize=10)

axs[1, 0].plot(3 * np.cos(an), 3 * np.sin(an))
axs[1, 0].axis('equal')
axs[1, 0].set(xlim=(-3, 3), ylim=(-3, 3))
axs[1, 0].set_title('still a circle, even after changing limits', fontsize=10)

axs[1, 1].plot(3 * np.cos(an), 3 * np.sin(an))
axs[1, 1].set_aspect('equal', 'box')
axs[1, 1].set_title('still a circle, auto-adjusted data limits', fontsize=10)

fig.tight_layout()

plt.show()

enter image description here