【问题标题】:Plotting points on circle's circumference but coming up as a ellipse在圆的圆周上绘制点但以椭圆的形式出现
【发布时间】:2019-12-01 00:36:53
【问题描述】:

我已经编写了一个代码(在 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)

【问题讨论】:

    标签: python arrays matplotlib geometry scatter-plot


    【解决方案1】:

    您可以使用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()
    

    【讨论】:

      【解决方案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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多