Scatter Plot on Polar Axis

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} matplotlib/figures_axes -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} matplotlib/scatter_plots -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/tuples -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/importing_modules -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/standard_libraries -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/math_random -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/numerical_computing -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} python/data_visualization -.-> lab-48875{{"`Scatter Plot on Polar Axis`"}} end

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.

Other Matplotlib Tutorials you may like