Plot 3D Parametric Curves with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

This lab will demonstrate how to plot a parametric curve in 3D using Matplotlib. The curve will be defined by three equations:

x = r * sin(theta)

y = r * cos(theta)

z = z

where r and z are defined as:

r = z^2 + 1

z is a range of values from -2 to 2, and theta is a range of values from -4π to 4π.

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`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} matplotlib/line_plots -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} matplotlib/legend_config -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} python/tuples -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} python/importing_modules -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} python/numerical_computing -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} python/data_visualization -.-> lab-48808{{"`Plot 3D Parametric Curves with Matplotlib`"}} end

Import necessary libraries

We will begin by importing the necessary libraries: Matplotlib and NumPy. Matplotlib will be used to create the 3D plot, and NumPy will be used to generate the values for x, y, and z.

import matplotlib.pyplot as plt
import numpy as np

Create a 3D plot

Next, we will create a 3D plot using Matplotlib. We will also create an axis object to add labels and legends to the plot.

ax = plt.figure().add_subplot(projection='3d')

Define the values for x, y, and z

We will generate the values for x, y, and z using NumPy. We will first define the range of values for theta and z. Then, we will use these values to generate the values for r, x, and y.

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

Plot the parametric curve

Now that we have generated the values for x, y, and z, we can plot the parametric curve using the plot() method in Matplotlib.

ax.plot(x, y, z, label='parametric curve')

Add labels and legends to the plot

Finally, we will add labels and legends to the plot using the legend() method.

ax.legend()

Summary

In this lab, we learned how to plot a parametric curve in 3D using Matplotlib. We defined the curve using three equations and generated the values for x, y, and z using NumPy. We then plotted the curve and added labels and legends to the plot.

Other Python Tutorials you may like