Introduction
In this tutorial, we will learn how to create scatter plots on a polar axis using Matplotlib in Python. A polar plot is a graphical representation of data that is displayed in polar coordinates. It is useful when the data is cyclical or circular in nature, such as data measured over time or direction.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import necessary libraries
We need to import the Matplotlib and NumPy libraries to create the scatter plot on a polar axis. We will also set the random seed for reproducibility.
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
Generate random data
We will generate random data for the scatter plot using NumPy. We will create 150 data points with random radius and angle values, and compute the area and color of each point.
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta
Create a scatter plot on a polar axis
We will create a scatter plot on a polar axis using the plt.scatter() function. We will set the projection parameter to 'polar', and pass in the radius, angle, color, and area values as parameters.
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
Create a scatter plot on a polar axis with offset origin
We can create a scatter plot on a polar axis with an offset origin by setting the set_rorigin() and set_theta_zero_location() methods of the PolarAxes object. We will set the origin radius to -2.5 and the theta zero location to 'W' with an offset of 10.
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
ax.set_rorigin(-2.5)
ax.set_theta_zero_location('W', offset=10)
Create a scatter plot on a polar axis confined to a sector
We can create a scatter plot on a polar axis confined to a sector by setting the set_thetamin() and set_thetamax() methods of the PolarAxes object. We will set the theta start and end limits to 45 and 135, respectively.
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
ax.set_thetamin(45)
ax.set_thetamax(135)
Summary
In this tutorial, we learned how to create scatter plots on a polar axis using Matplotlib in Python. We generated random data, created scatter plots on a polar axis, created scatter plots on a polar axis with an offset origin, and created scatter plots on a polar axis confined to a sector. Polar plots are useful for displaying cyclical or circular data, such as data measured over time or direction.