3D Surface Plotting with Polar Coordinates

PythonPythonBeginner
Practice Now

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

Introduction

This lab is a step-by-step guide to creating a 3D surface with polar coordinates using Python Matplotlib library. This lab assumes basic knowledge of Python programming and the Matplotlib library.

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/DataStructuresGroup -.-> python/sets("`Sets`") 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-48972{{"`3D Surface Plotting with Polar Coordinates`"}} matplotlib/figures_axes -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} matplotlib/3d_plots -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} python/tuples -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} python/sets -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} python/importing_modules -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} python/numerical_computing -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} python/data_visualization -.-> lab-48972{{"`3D Surface Plotting with Polar Coordinates`"}} end

Import Required Libraries

We will begin by importing the required libraries for this lab, which include Matplotlib and NumPy. Matplotlib is a plotting library for Python, while NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices.

import matplotlib.pyplot as plt
import numpy as np

Create the Mesh

Next, we will create the mesh in polar coordinates and compute corresponding Z. We will create an array of radius values r, an array of angle values p, and then use NumPy's meshgrid() function to create a grid of R and P values. Finally, we will use the Z equation to compute the height of each point on the surface.

r = np.linspace(0, 1.25, 50)
p = np.linspace(0, 2*np.pi, 50)
R, P = np.meshgrid(r, p)
Z = ((R**2 - 1)**2)

Express the Mesh in Cartesian System

Now, we will express the mesh in the cartesian system using NumPy's cos() and sin() functions.

X, Y = R*np.cos(P), R*np.sin(P)

Plot the Surface

In this step, we will plot the surface using Matplotlib's plot_surface() function. We will use the colormap YlGnBu_r to set the color of the surface.

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)

Tweak the Limits and Add Labels

Finally, we will tweak the limits of the plot and add axis labels using Matplotlib's set_zlim() and set_xlabel(), set_ylabel(), set_zlabel() functions. We will also use LaTeX math mode to write the axis labels.

ax.set_zlim(0, 1)
ax.set_xlabel(r'$\phi_\mathrm{real}$')
ax.set_ylabel(r'$\phi_\mathrm{im}$')
ax.set_zlabel(r'$V(\phi)$')

Summary

In this lab, we have learned how to create a 3D surface with polar coordinates using Python Matplotlib library. We started by importing the required libraries, created a mesh in polar coordinates, expressed the mesh in the cartesian system, plotted the surface, and finally tweaked the limits and added axis labels.

Other Python Tutorials you may like