Introduction
In this lab, we will learn how to create a 3D contour plot using Matplotlib library in Python. A contour plot is a graphical representation of a 3D surface in which contours are plotted on a 2D plane. Contour plots are useful to visualize the variation of a variable with respect to two other variables.
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.
Import Libraries
We will begin by importing the necessary libraries for creating the 3D contour plot. We will be using the matplotlib and mpl_toolkits libraries.
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
Create Figure and Subplot
Next, we create a figure and subplot to hold our 3D contour plot.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Get Test Data
We will use the axes3d.get_test_data() function to get some test data to plot.
X, Y, Z = axes3d.get_test_data(0.05)
Create Contour Plot
Now we can create a 3D contour plot of the test data using the ax.contour() function.
ax.contour(X, Y, Z, cmap=cm.coolwarm)
Customize Plot
We can customize the plot by adding labels to the axes and adjusting the viewing angle.
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.view_init(elev=30, azim=120)
Show Plot
Finally, we use the plt.show() function to display the 3D contour plot.
plt.show()
Summary
In this lab, we learned how to create a 3D contour plot using Matplotlib library in Python. We imported the necessary libraries, created a figure and subplot, got test data, created a contour plot, customized the plot, and displayed the plot. Contour plots are useful for visualizing the variation of a variable with respect to two other variables.