Matplotlib - 在极坐标图中绘制一个光滑的圆

时间:2013-11-07 04:03:27

标签: python matplotlib geometry polar-coordinates

我非常喜欢matplotlib的极地情节,并希望继续使用它(因为我的数据点无论如何以极坐标给出,我的环境是圆形的。)

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

通常,我会这样做:

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

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

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

谢谢!

1 个答案:

答案 0 :(得分:15)

您可以设置transform的{​​{1}}参数:

Circle

输出:

enter image description here

如果您不喜欢使用私有属性,请

%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)