Matplotlib - Drawing a smooth circle in a polar plot

╄→尐↘猪︶ㄣ 提交于 2019-12-17 19:39:00

问题


I really like the polar plot of matplotlib and would love to keep working with it (since my data points are given in polar coordinates anyway and my environment is circular).

However, in the plot, I would like to add circles of given radii at specific points.

Usually, I would do:

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

However, in polar coordinates, I cannot use circle, since it assumes rectangular coordinates.

Ideas I have come up with are: generating an array of points with constant radial coordinate and an angular coordinate in [0, 2PI] or completely switching to rectangular coordinates. Both solutions are not really satisfactory - can one do any better with matplotlib?

Thanks!


回答1:


You can set transform argument of the 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)

output:

or transform=ax.transProjectionAffine + ax.transAxes if you don't like using the private attribute.



来源:https://stackoverflow.com/questions/19827792/matplotlib-drawing-a-smooth-circle-in-a-polar-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!