我编写了一个代码(在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)
该程序运行完美,但是由于某种原因,圆具有更多的椭圆形状。另外,图中的点之间的距离不相等:
(在图片中您看不到轴,但它们在x和y上都从-100变为100)
答案 0 :(得分:3)
您可以使用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()
答案 1 :(得分:2)
检查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()