3D Plots as Subplots

PythonPythonBeginner
Practice Now

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

Introduction

In data analysis, it's often necessary to create 3D plots to visualize data. In Matplotlib, we can create 3D plots as subplots to compare different 3D data. This lab will demonstrate how to create 3D plots as subplots 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`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") 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/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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/comments -.-> lab-48967{{"`3D Plots as Subplots`"}} python/with_statement -.-> lab-48967{{"`3D Plots as Subplots`"}} matplotlib/importing_matplotlib -.-> lab-48967{{"`3D Plots as Subplots`"}} matplotlib/figures_axes -.-> lab-48967{{"`3D Plots as Subplots`"}} matplotlib/3d_plots -.-> lab-48967{{"`3D Plots as Subplots`"}} python/booleans -.-> lab-48967{{"`3D Plots as Subplots`"}} python/for_loops -.-> lab-48967{{"`3D Plots as Subplots`"}} python/tuples -.-> lab-48967{{"`3D Plots as Subplots`"}} python/importing_modules -.-> lab-48967{{"`3D Plots as Subplots`"}} python/using_packages -.-> lab-48967{{"`3D Plots as Subplots`"}} python/numerical_computing -.-> lab-48967{{"`3D Plots as Subplots`"}} python/data_visualization -.-> lab-48967{{"`3D Plots as Subplots`"}} end

Import Libraries

Before we start, we need to import the libraries that we will use in this lab. We will use Matplotlib, NumPy, and Axes3D from mpl_toolkits.mplot3d.

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

Create the Figure and Subplots

We will create a figure with two subplots. The first subplot will be a 3D surface plot, and the second subplot will be a 3D wireframe plot.

## Create a figure with two subplots
fig = plt.figure(figsize=plt.figaspect(0.5))

## Add the first subplot with 3D projection
ax1 = fig.add_subplot(1, 2, 1, projection='3d')

## Add the second subplot with 3D projection
ax2 = fig.add_subplot(1, 2, 2, projection='3d')

Create the 3D Surface Plot

We will create a 3D surface plot for the first subplot. We will use NumPy to create the data for the plot.

## Create data for the 3D surface plot
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

## Plot the 3D surface plot
surf = ax1.plot_surface(X, Y, Z, cmap='coolwarm', linewidth=0, antialiased=False)

## Add a color bar to the plot
fig.colorbar(surf, shrink=0.5, aspect=10)

## Set the limits for the z-axis
ax1.set_zlim(-1.01, 1.01)

Create the 3D Wireframe Plot

We will create a 3D wireframe plot for the second subplot. We will use the get_test_data function from mpl_toolkits.mplot3d.axes3d to create the data for the plot.

## Create data for the 3D wireframe plot
X, Y, Z = Axes3D.get_test_data(0.05)

## Plot the 3D wireframe plot
ax2.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

Show the Plot

We will use the plt.show() function to display the plot.

plt.show()

Summary

In this lab, we learned how to create 3D plots as subplots using Matplotlib. We created a figure with two subplots, a 3D surface plot, and a 3D wireframe plot. We used NumPy to create the data for the 3D surface plot and the get_test_data function from mpl_toolkits.mplot3d.axes3d to create the data for the 3D wireframe plot. We also added a color bar to the 3D surface plot and set the limits for the z-axis.

Other Python Tutorials you may like