Create 3D Plots with Matplotlib in Python

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through the process of creating a 3D plot using Matplotlib in Python.

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/AdvancedPlottingGroup(["`Advanced Plotting`"]) 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/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") 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 matplotlib/importing_matplotlib -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} matplotlib/figures_axes -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} matplotlib/3d_plots -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} python/tuples -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} python/importing_modules -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} python/using_packages -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} python/numerical_computing -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} python/data_visualization -.-> lab-48848{{"`Create 3D Plots with Matplotlib in Python`"}} end

Import Necessary Libraries

We begin by importing the necessary libraries. In this case, we need NumPy and Matplotlib.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

Create Data

Next, we create the data that we will use in our plot. In this example, we will be using NumPy to generate the data.

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))

Create the Figure and Axes Objects

Now, we create a figure and axes object that we will use to create the plot.

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

Create the Plot

Finally, we create the plot using the data and the axes object we just created.

ax.plot_surface(X, Y, Z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

Display the Plot

We display the plot using the plt.show() function.

plt.show()

Summary

In this lab, we learned how to create a 3D plot using Matplotlib in Python. We started by importing the necessary libraries, then created the data, figure and axes objects, and finally created and displayed the plot.

Other Python Tutorials you may like