Contents

## Introduction

In python, we have discussed many concepts. We will be discussing the concept of the **matplotlib.pyplot.polar() function.** The matplotlib is the python package that is used for creating static, animated, and interactive visualizations in Python. Inside it, Pyplot is a Matplotlib module that provides a MATLAB-like interface.

## What is a Matplotlib?

**The Matplotlib is the python package used to create static, animated, and interactive visualizations in Python**. Matplotlib is designed with the ability to use Python and the advantage of being free and open-source. It can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interfaces, etc.

## What is Pyplot?

**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?

**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. The function has two parameters, i.e., theta and r.

## Syntax for matplotlib.pyplot.polar() function

**matplotlib.pyplot.polar(theta, r, **kwargs)**

## Parameters of matplotlib.pyplot.polar() function

**Theta:**This is the angle at which we want to draw the curve.**r:**It is the distance.

## Return value of matplotlib.pyplot.polar() function

There is no return value in this function.

## 5 Examples Demonstrating Use of matplotlib.pyplot.polar() function

Here, we will be discussing how we can use the matplotlib.pyplot.polar() function in the pyplot module in the matplotlib library.

### 1. Draw a circle using matplotlib.pyplot.polar() function

A circle is a shape consisting of all points in a plane that are a given distance called radius from a given point, the center. So in this example, we will be seeing how to draw the circle with the help of the polar() function. Let us look at the example for understanding the concept in detail.

```
#import numpy and matplotlib library
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection = 'polar')
r = 3
rads = np.arange(0, (2 * np.pi), 0.01)
# plotting the circle
for i in rads:
plt.polar(i, r, 'g.')
# display the Polar plot
plt.show()
```

**Output:**

**Explanation:**

- Firstly, we will import the numpy library with an alias name as np.
- Then, we will import the matplotlib.pyplot library.
- After that, we have plt. axes() function in which we have set projection equals to polar.
- Then, we have set the value of r equal to 3, which is a radius.
- Then, we have applied the arange function from the numpy library.
- After that, we have applied the for loop for plotting the circle.
- At last, we have done plt. show() which gives the circle as the output.
- Hence, you can see the circle as the output.

### 2. Draw an ellipse using matplotlib.pyplot.polar() function

An ellipse is a locus of a point moving in a plane such that the sum of its distances from two other points is called foci which is constant. So in this example, we will see how to draw the ellipse with the help of the polar() function. Let us look at the example for understanding the concept in detail.

```
#import numpy, matplotlib, and math library
import numpy as np
import matplotlib.pyplot as plt
import math
plt.axes(projection = 'polar')
a = 5
b = 2
rad = np.arange(0, (2 * np.pi), 0.01)
# plotting the ellipse
for i in rad:
r = (a*b)/math.sqrt((a*np.sin(i))**2 + (b*np.cos(i))**2)
plt.polar(i, r, 'g.')
# display the polar plot
plt.show()
```

**Output:**

**Explanation:**

- Firstly, we will import the numpy library with an alias name as np.
- Then, we will import the matplotlib.pyplot library.
- After that, we have plt. axes() function in which we have set projection equals to polar.
- Then, we have set the value of a and b equal to 5 and 2 for the major and minor axis.
- Then, we have applied the arange function from the numpy library.
- After that, we have applied the for loop for plotting the ellipse.
- At last, we have done plt. show() which gives the ellipse as the output.
- Hence, you can see the ellipse as the output.

### 3. Draw a cardioid using matplotlib.pyplot.polar() function

A cardioid is the locus point on the circumference of a circle as it rolls around another identical circle. Here, r is defined as : a+acos(theta). So in this example, we will see how to draw the cardioid with the help of the polar() function. Let us look at the example for understanding the concept in detail.

```
import numpy as np
import matplotlib.pyplot as plt
import math
plt.axes(projection = 'polar')
a=3
rad = np.arange(0, (2 * np.pi), 0.01)
# plotting the cardioid
for i in rad:
r = a + (a*np.cos(i))
plt.polar(i,r,'g.')
# display the polar plot
plt.show()
```

**Output:**

**Explanation:**

- Firstly, we will import the numpy library with an alias name as np.
- Then, we will import the matplotlib.pyplot library.
- After that, we have plt. axes() function in which we have set projection equals to polar.
- Then, we have set the value of an equal to 3, which is the axis.
- Then, we have applied the arange function from the numpy library.
- After that, we have applied the for loop for plotting the cardioid.
- At last, we have done plt. show() which gives the cardioid as the output.
- Hence, you can see the cardioid as the output.

### 4. Draw a **Archimedean spiral** using matplotlib.pyplot.polar() function

An Archimedean spiral is the locus point moving uniformly on a straight line, which itself is turning uniformly about one of its endpoints. Here, r is defined as theta. So in this example, we will see how to draw the archimedean spiral with the help of the polar() function. Let us look at the example for understanding the concept in detail.

```
#import numpy and matplotlib library
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection = 'polar')
rad = np.arange(0, 2 * np.pi, 0.01)
# plotting the spiral
for i in rad:
r = i
plt.polar(i, r, 'g.')
# display the polar plot
plt.show()
```

**Output:**

**Explanation:**

- Firstly, we will import the numpy library with an alias name as np.
- Then, we will import the matplotlib.pyplot library.
- After that, we have plt. axes() function in which we have set projection equals to polar.
- Then, we have applied the arange function from the numpy library.
- After that, we have applied the for loop for plotting the archimedean spiral.
- At last, we have done plt. show() which gives the archimedean spiral as the output.
- Hence, you can see the archimedean spiral as the output.

### 5. Draw a **Rhodonea** using matplotlib.pyplot.polar() function

A Rhodonea is also called a Rose curve, is a rose-shaped sinusoid plotted in polar coordinates. Here, r is defined as a*cos(ntheta). So in this example, we will see how to draw the rhodonea with the help of the polar() function. Let us look at the example for understanding the concept in detail.

```
#import numpy and matplotlib library
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection='polar')
a = 1
n = 4
rad = np.arange(0, 2 * np.pi, 0.001)
# plotting the rose
for i in rad:
r = a * np.cos(n*i)
plt.polar(i, r, 'g.')
# display the polar plot
plt.show()
```

**Output:**

**Explanation:**

- Firstly, we will import the numpy library with an alias name as np.
- Then, we will import the matplotlib.pyplot library.
- After that, we have plt. axes function in which we have set projection equals to polar.
- Then, we have set the value of a and n equal to 1 and 4 for the length and no of petals.
- Then, we have applied the arange function from the numpy library.
- After that, we have applied the for loop for plotting the rhodonea.
- At last, we have done plt. show() which gives the rhodonea as the output.
- Hence, you can see the rhodonea as the output.

## Conclusion

In this tutorial, we have learned about the concept of matplotlib.pyplot.polar() function. We have all the definitions of matplotlib, pyplot, and polar function with the syntax and parameters. We have also seen all the examples by which we have drawn different shapes like circles, ellipse. Archimedean spiral and rhodonea, etc. All the shapes are explained in detail with the help of their codes. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

## Important FAQs on matplotlib.pyplot.polar() function

### Q1. How do we draw a scatter plot on the polar axis?

```
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
# Compute areas and colors
N = 100
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 100 * r**2
colors = theta
plt.axes(projection='polar')
c = plt.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
```

**Output:**

### Q2. Can we draw a python 3d polar plot?

**Yes, we can draw a python 3d polar plot.**

```
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = np.linspace(0, 1.25, 30)
p = np.linspace(0, 2*np.pi, 20)
R, P = np.meshgrid(r, p)
Z = ((R**2 - 0.5)**2)
X, Y = R*np.cos(P), R*np.sin(P)
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
ax.set_zlim(0, 1)
ax.set_xlabel()
ax.set_ylabel()
ax.set_zlabel()
plt.show()
```

**Output:**

### Q3. How to set axis labels of matplotlib polar plot?

**We can set the axis labels in the polar plot with the help of linspace() in the numpy module and sin() function in the numpy module.**

```
import matplotlib.pyplot as plt
import numpy as np
theta = np.linspace(0, np.pi, 100)
r = np.sin(theta)
plt.polar(theta, r)
# Change tick labels here
plt.yticks([1, 2, 3], ["a", "b", "c"])
plt.show()
```

**Output:**

### Q4. How to plot 0 at top of the polar plot in Matplotlib

**We can do this by setting the set_theta_zero_location(“N”). this will set the axis label from the north starting from the 0 degree.**

```
import matplotlib.pyplot as plt
ax = plt.subplot(polar=True)
ax.set_theta_zero_location("N")
plt.show()
import matplotlib.pyplot as plt
ax = plt.subplot(polar=True)
ax.set_theta_zero_location("W")
plt.show()
```

**Output:**

Thank you for this clear and well structured overview. Most other websites couldn’t explain this well. Even the documention says only plt.polar(**kwargs) so I didn’t know if r or theta is the fist argument.

I’m glad you liked it!

Regards,

Pratik