在圆的圆周上绘制点,但以椭圆形出现
Plotting points on circle's circumference but coming up as a ellipse
我做了一个代码(在python),在圆周上每隔10度做一个点:
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)
程序运行完美,但由于某种原因,圆圈更像是椭圆形。此外,图表上的点之间的距离不相等:
圆周上每10度点的散点图:
(在图片中你看不到轴,但它们在 x 和 y 上都从 -100 到 100)
您可以使用 plt.axis('equal')
来保持绘图的正确纵横比。
关于点间距不均匀的问题:您以度为单位给出 theta
,但三角函数期望它们的输入以弧度为单位。您可以将度数转换为弧度乘以 180 再除以 Pi,如代码所示。
我还稍微重写了代码以更好地利用 numpy's broadcasting 技巧。
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()
检查 https://matplotlib.org/gallery/subplots_axes_and_figures/axis_equal_demo.html 正是这样。
为不熟悉如何阅读文档的人复制这里。
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()
我做了一个代码(在python),在圆周上每隔10度做一个点:
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)
程序运行完美,但由于某种原因,圆圈更像是椭圆形。此外,图表上的点之间的距离不相等:
圆周上每10度点的散点图:
(在图片中你看不到轴,但它们在 x 和 y 上都从 -100 到 100)
您可以使用 plt.axis('equal')
来保持绘图的正确纵横比。
关于点间距不均匀的问题:您以度为单位给出 theta
,但三角函数期望它们的输入以弧度为单位。您可以将度数转换为弧度乘以 180 再除以 Pi,如代码所示。
我还稍微重写了代码以更好地利用 numpy's broadcasting 技巧。
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()
检查 https://matplotlib.org/gallery/subplots_axes_and_figures/axis_equal_demo.html 正是这样。
为不熟悉如何阅读文档的人复制这里。
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()