Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

python - Matplotlib - Drawing a smooth circle in a polar plot

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...