How to make a polar plot with Matplotlib using Python

2 min

Polar plots are really useful when you need to show something on a rotating axis.

Especially when your are working with geometry problems or when displaying world maps.

Let me show you

Here is an example of a simple line on a simple 2d axis.

# To generate data
import numpy as np

# To plot it
import matplotlib.pyplot as plt

# We generate our sample data
theta = np.arange(0,3,.01) * np.pi
rho = np.sin(4*theta)*np.cos(4*theta)

# We create our canvas
fig, axes = plt.subplots(1,
                         1,
                         figsize=(8,6))

# We plot a line plot on a polar axis
axes.plot(theta, rho)

# We change the number of half
axes.grid(True)

axes.set_title("A simple line plot")
plt.show()
A normal line plot
The simple line plot

Here is how to plot it on a polar axis

# To generate data
import numpy as np

# To plot it
import matplotlib.pyplot as plt

# We generate our sample data
theta = np.arange(0,3,.01) * np.pi
rho = np.sin(4*theta)*np.cos(4*theta)

# We create our canvas
fig, axes = plt.subplots(1,
                         1,
                         figsize=(8,8),
                         # We specify here the type of projection we want
                         subplot_kw={'projection': 'polar'})

# We plot a line plot on a polar axis
axes.plot(theta, rho)

# We change the number of half
axes.grid(True)

axes.set_title("A line plot on a polar axis")
fig.tight_layout()
plt.show()

As you can see using the subplots_kw={'projection': 'polar'} it is pretty simple to go from a simple line plot to a polar projection.

A line plot on a polar axis

Here you are, as you can see we projected the same simple line but on a polar axis.

More on Matplotlib

If you like what you've just read and want to know more about the Matplotlib library (e.g. how to add labels, plot different types of plots, etc...) check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.