我真的很喜欢matplotlib的极坐标图,并希望继续使用它(因为无论如何我的数据点都是在极坐标中给出的,而且我的环境是圆形的)。
但是,在图中,我想在特定点添加给定半径的圆。
通常,我会这样做:
ax = plt.subplot(111)
ax.scatter(data)
circle = plt.Circle((0,0), 0.5)
ax.add_artist(circle)
plt.show()
但是,在极坐标中,我不能使用圆,因为它采用直角坐标。
我想到的想法是:在[0,2PI]中生成具有恒定径向坐标和角坐标的点的数组,或完全切换为直角坐标。两种解决方案都不尽如人意-使用matplotlib可以做得更好吗?
谢谢!
您可以设置transform
参数Circle
:
%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
如果您不喜欢使用private属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句