Create Polar Plots with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used for creating static, animated, and interactive visualizations in Python. One of the key features of Matplotlib is its ability to create 2D and 3D plots of all types and styles, including scatter plots, line plots, and bar charts. In this lab, you will learn how to create a polar curve in a rectangular box using Matplotlib.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} matplotlib/grid_config -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/booleans -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/lists -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/tuples -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/sets -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/importing_modules -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/using_packages -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/numerical_computing -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} python/data_visualization -.-> lab-48677{{"`Create Polar Plots with Matplotlib`"}} end

Import Required Libraries

In this step, we will import the required libraries for creating the polar curve. We will use numpy for numerical computing and matplotlib for creating the plot.

import matplotlib.pyplot as plt
import numpy as np

Define the Polar Axes

In this step, we will define the polar axes and set the scaling factor. We will use PolarAxes.PolarTransform() to define the polar axes.

from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D

## Define the polar axes
tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform()

Define the Grid Helper

In this step, we will define the grid helper that will be used to create the polar curve. We will use GridHelperCurveLinear to define the grid helper.

from mpl_toolkits.axisartist import GridHelperCurveLinear, HostAxes
import mpl_toolkits.axisartist.angle_helper as angle_helper

## Define the grid helper
extreme_finder = angle_helper.ExtremeFinderCycle(20,
                                                 20,
                                                 lon_cycle=360,
                                                 lat_cycle=None,
                                                 lon_minmax=None,
                                                 lat_minmax=(0, np.inf),
                                                 )
grid_locator1 = angle_helper.LocatorDMS(12)
tick_formatter1 = angle_helper.FormatterDMS()

grid_helper = GridHelperCurveLinear(tr,
                                    extreme_finder=extreme_finder,
                                    grid_locator1=grid_locator1,
                                    tick_formatter1=tick_formatter1
                                    )

Create the Host Axes

In this step, we will create the host axes and set the grid helper. We will use fig.add_subplot() to create the host axes.

## Create the host axes
fig = plt.figure(figsize=(5, 5))
ax1 = fig.add_subplot(axes_class=HostAxes, grid_helper=grid_helper)

Create Floating Axes

In this step, we will create two floating axes that will be used to display the polar curve in a rectangular box. We will use new_floating_axis() to create the floating axes.

## Create the floating axes
ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60)
axis.label.set_text(r"$\theta = 60^{\circ}$")
axis.label.set_visible(True)

ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6)
axis.label.set_text(r"$r = 6$")

Set the Limits and Display the Grid

In this step, we will set the limits for the axes and display the grid. We will use set_aspect() to set the aspect ratio of the axes and grid() to display the grid.

## Set the limits and display the grid
ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
ax1.grid(True)

Display the Polar Curve

In this step, we will display the polar curve in the rectangular box. We will use plt.show() to display the plot.

## Display the polar curve
plt.show()

Summary

In this lab, you learned how to create a polar curve in a rectangular box using Matplotlib. You learned how to define the polar axes and the grid helper, create the host axes, create the floating axes, set the limits, display the grid, and display the polar curve. With this knowledge, you can create a wide variety of polar curves in rectangular boxes using Matplotlib.

Other Python Tutorials you may like