3D Plot Projection Types

PythonPythonBeginner
Practice Now

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

Introduction

This lab demonstrates the different camera projections for 3D plots, and the effects of changing the focal length for a perspective projection using Python 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/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`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48883{{"`3D Plot Projection Types`"}} matplotlib/importing_matplotlib -.-> lab-48883{{"`3D Plot Projection Types`"}} matplotlib/figures_axes -.-> lab-48883{{"`3D Plot Projection Types`"}} matplotlib/3d_plots -.-> lab-48883{{"`3D Plot Projection Types`"}} python/for_loops -.-> lab-48883{{"`3D Plot Projection Types`"}} python/lists -.-> lab-48883{{"`3D Plot Projection Types`"}} python/tuples -.-> lab-48883{{"`3D Plot Projection Types`"}} python/dictionaries -.-> lab-48883{{"`3D Plot Projection Types`"}} python/importing_modules -.-> lab-48883{{"`3D Plot Projection Types`"}} python/using_packages -.-> lab-48883{{"`3D Plot Projection Types`"}} python/data_visualization -.-> lab-48883{{"`3D Plot Projection Types`"}} end

Import Libraries

First, import the necessary libraries, including Matplotlib and Axes3D from mpl_toolkits.mplot3d.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

Get Data

Get the test data for the 3D plot.

X, Y, Z = axes3d.get_test_data(0.05)

Create a Figure with Subplots

Create a figure with three subplots using plt.subplots.

fig, axs = plt.subplots(1, 3, subplot_kw={'projection': '3d'})

Plot the Data

Plot the data on each of the three subplots using plot_wireframe.

for ax in axs:
    ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

Set the Orthographic Projection

Set the first subplot to use an orthographic projection with a FOV of 0 degrees and a focal_length of infinity.

axs[0].set_proj_type('ortho')  ## FOV = 0 deg
axs[0].set_title("'ortho'\nfocal_length = ∞", fontsize=10)

Set the Perspective Projections

Set the second subplot to use a perspective projection with the default FOV of 90 degrees and a focal_length of 1.

axs[1].set_proj_type('persp')  ## FOV = 90 deg
axs[1].set_title("'persp'\nfocal_length = 1 (default)", fontsize=10)

Set the third subplot to use a perspective projection with a FOV of 157.4 degrees and a focal_length of 0.2.

axs[2].set_proj_type('persp', focal_length=0.2)  ## FOV = 157.4 deg
axs[2].set_title("'persp'\nfocal_length = 0.2", fontsize=10)

Show the Plot

Display the plot using plt.show().

plt.show()

Summary

This lab demonstrated how to create a 3D plot using Python Matplotlib and how to change the projection type and focal length of the plot. The orthographic projection flattens the image while the perspective projection exaggerates the perspective and gives the image more apparent depth. The default focal length of 1 corresponds to a Field of View (FOV) of 90 deg.

Other Python Tutorials you may like