Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 699
limbonic Avatar asked Nov 07 '13 04:11

limbonic


People also ask

How do you plot a circle in a polar plot?

The easiest approach would be to use the rectangle function with the curvature option to make circles and to fill all of them. Plot them in order from bottom to top with the top most circle being white.

How to plot the curves in polar coordinates in Matplotlib?

The matplotlib.pyplot.polar () function in pyplot module of matplotlib python library is used to plot the curves in polar coordinates. The function is used to draw circles, ellipse, archimedean spiral, rhodonea, and cardioid, etc.

How to plot a circle in Python using matplotlib?

A Circle is a mathematical figure formed by joining all points lying on the same plane and are at equal distance from a given point. We can plot a circle in python using Matplotlib. There are multiple ways to plot a Circle in python using Matplotlib. Method 1: Using matplotlib.patches.Circle() function.

How to plot a smooth curve in Matplotlib?

How to Plot a Smooth Curve in Matplotlib? 1 Python. import numpy as np. import matplotlib.pyplot as plt. x = np.array ( [ 1, 2, 3, 4, 5, 6, 7, 8 ]) y = np.array ( [ 20, 30, 5, 12, 39, 48, 50, 3 ... 2 Python. 3 Python.

What is Pyplot in Matplotlib?

The Pyplot is a Matplotlib module that is used to provides a MATLAB-like interface. The various plots which can be utilized using Pyplot are Line Plot, Histogram, Scatter, 3D Plot, Image, Contour, and Polar. What is pyplot.polar () fuction in matplotlib?


1 Answers

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.

like image 76
HYRY Avatar answered Oct 11 '22 23:10

HYRY