Create Radian Plots with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will show you how to create a plot with radians using the Python Matplotlib package. You will learn how to use the unit class to determine the tick locating, formatting, and axis labeling.

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/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") 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/with_statement -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} matplotlib/line_plots -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/for_loops -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/list_comprehensions -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/lists -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/tuples -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/importing_modules -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/using_packages -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/numerical_computing -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} python/data_visualization -.-> lab-48897{{"`Create Radian Plots with Matplotlib`"}} end

Import necessary packages

First, import the necessary packages, including matplotlib.pyplot and numpy.

import matplotlib.pyplot as plt
import numpy as np

Create data

Create an array of values between 0 and 15 in increments of 0.01, and convert them to radians using the radians function from the basic_units package.

from basic_units import radians
x = [val*radians for val in np.arange(0, 15, 0.01)]

Create a figure

Create a figure with two subplots using the subplots function from matplotlib.pyplot.

fig, axs = plt.subplots(2)

Plot data in the first subplot

Plot the cosine of the x values in the first subplot using the plot function from matplotlib.pyplot. Use the xunits parameter to specify that the x-axis should be in radians.

from basic_units import cos
axs[0].plot(x, cos(x), xunits=radians)

Plot data in the second subplot

Plot the cosine of the x values in the second subplot using the plot function from matplotlib.pyplot. Use the xunits parameter to specify that the x-axis should be in degrees.

from basic_units import degrees
axs[1].plot(x, cos(x), xunits=degrees)

Add labels and adjust layout

Add a title and axis labels to the subplots using the title, xlabel, and ylabel functions from matplotlib.pyplot. Adjust the layout of the subplots using the tight_layout function.

axs[0].set_title('Cosine with Radian X-Axis')
axs[0].set_xlabel('Radians')
axs[0].set_ylabel('Cosine')
axs[1].set_title('Cosine with Degree X-Axis')
axs[1].set_xlabel('Degrees')
axs[1].set_ylabel('Cosine')
fig.tight_layout()

Display the plot

Display the plot using the show function from matplotlib.pyplot.

plt.show()

Summary

In this tutorial, you learned how to create a plot with radians using the Python Matplotlib package. You used the unit class to determine the tick locating, formatting, and axis labeling. You also learned how to create subplots, plot data, add labels, and adjust the layout of the plot.

Other Python Tutorials you may like