【问题标题】:Matplotlib - Drawing a smooth circle in a polar plotMatplotlib - 在极坐标图中绘制一个平滑的圆
【发布时间】:2013-11-07 04:03:26
【问题描述】:

我真的很喜欢 matplotlib 的极坐标图,并且很想继续使用它(因为无论如何我的数据点都是在极坐标中给出的,而且我的环境是圆形的)。

但是,在情节中,我想在特定点添加给定半径的圆。

通常,我会这样做:

 ax = plt.subplot(111)
 ax.scatter(data)
 circle = plt.Circle((0,0), 0.5)
 ax.add_artist(circle)
 plt.show()

但是,在极坐标中,我不能使用圆形,因为它采用直角坐标。

我想出的想法是:生成具有恒定径向坐标和 [0, 2PI] 中的角坐标的点数组或完全切换到直角坐标。两种解决方案都不太令人满意 - 使用 matplotlib 可以做得更好吗?

谢谢!

【问题讨论】:

    标签: python matplotlib geometry polar-coordinates


    【解决方案1】:

    你可以设置Circletransform参数:

    %matplotlib inline
    import pylab as pl
    import numpy as np
    
    N = 100
    theta = np.random.rand(N)*np.pi*2
    r = np.cos(theta*2) + np.random.randn(N)*0.1
    
    ax = pl.subplot(111, polar=True)
    ax.scatter(theta, r)
    circle = pl.Circle((0.5, 0.3), 0.2, transform=ax.transData._b, color="red", alpha=0.4)
    ax.add_artist(circle)
    

    输出:

    transform=ax.transProjectionAffine + ax.transAxes,如果您不喜欢使用私有属性。

    【讨论】:

    • 太好了,谢谢!对您的解决方案只有一句话(以防万一有人偶然发现):必须以笛卡尔坐标给出圆的原点。
    • 如果圆的中心以度为单位怎么办?
    • 如果你改变了theta的零位,例如:ax.set_theta_zero_location('N'),你应该使用transform = (Affine2D().rotate(ax._theta_offset.get_matrix()[0, 2]) + ax.transProjectionAffine + ax.transAxes)
    猜你喜欢
    • 2012-10-08
    • 2017-10-26
    • 2017-12-17
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多