In this tutorial, we’ll try to understand how to plot a 3D wireframe plot in python.
What is a wireframe plot?
Wireframe plot takes a grid of values and projects it onto the specified three-dimensional surface, and can make the resulting three-dimensional forms quite easy to visualize.
3D wireframe plot using the matplotlib.Axes.plot_wireframe()
method
To plot a 3D wireframe plot in Python, use the matplotlib Axes objects plot_wireframe()
method. The following is the syntax –
Basic Syntax:
Axes.plot_wireframe(X, Y, Z, **kwargs)
Parameters:
- X, Y, Z: Data values.
- rcount, ccount: Maximum number of samples used in each direction.
- rstride, cstride: Downsampling stride in each direction.
For more details about the parameters, refer this.
Now let us understand the usage of the above method with some examples.
Example 1
import matplotlib.pyplot as plt import numpy as np # defining variables x = np.linspace(-1, 2, 10) y = np.linspace(-1, 2, 10) x, y = np.meshgrid(x, y) z = np.sin(np.sqrt(x ** 2 + y ** 2)) #generating the figure and 3d axes fig = plt.figure(figsize = (6, 6)) ax = plt.axes(projection = '3d') #plotting the 3D wireframe plot ax.plot_wireframe(x, y, z) plt.show()
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
The steps followed in the above examples are:
- Import required modules.
- Define the variables.
- Generate the figure and 3d axes.
- Plot the 3D wireframe plot using
Axes.plot_wireframe
method.
Example 2
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits import mplot3d # defining variables x, y, z = mplot3d.axes3d.get_test_data(0.05) #generating the figure and 3d axes fig = plt.figure(figsize = (6, 6)) ax = plt.axes(projection = '3d') #plotting the 3D wireframe plot ax.plot_wireframe(x,y,z, rstride=2,cstride=2,color='green') plt.show()
Output:
The steps followed in the above examples are:
- Import required modules.
- Define the variables. Here we used
mplot3d
from thempl_toolkits
module to generate our 3d data. - Generate the figure and 3d axes.
- Plot the 3D wireframe plot using
Axes.plot_wireframe
method.
Example 3
You can customize the wireframe plot with additional parameters to the plot_wireframe()
function.
import matplotlib.pyplot as plt import numpy as np # defining variables x = np.outer(np.linspace(-2, 4, 8), np.ones(8)) y = x.copy().T z = np.cos(x ** 3 + y ** 4) #generating the figure and 3d axes fig = plt.figure(figsize = (6, 6)) ax = plt.axes(projection = '3d') #plotting the 3D wireframe plot ax.plot_wireframe(x, y, z, edgecolor ='orange') plt.show()
Output:
The steps followed in the above examples are:
- Import required modules.
- Define the variables.
- Generate the figure and 3d axes.
- Plot the 3D wireframe plot using
Axes.plot_wireframe
method and pass an additional parameter for theedgecolor
.
Example 4
You can also modify the viewing angle of the wireframe plot.
import matplotlib.pyplot as plt import numpy as np # defining variables x = np.outer(np.linspace(-2, 4, 8), np.ones(8)) y = x.copy().T z = np.cos(x ** 3 + y ** 4) #generating the figure and 3d axes fig = plt.figure(figsize = (6, 6)) ax = plt.axes(projection = '3d') #plotting the 3D wireframe plot ax.plot_wireframe(x, y, z, edgecolor ='orange') ax.view_init(70, 60) plt.show()
Output:
The steps followed in the above examples are:
- Import required modules.
- Define the variables.
- Generate the figure and 3d axes.
- Plot the 3D wireframe plot using
Axes.plot_wireframe
method. - Change the view angle by using
Axes.view_init
method (refer this).
Example 5
You can also add a contour plot at the base of a wireframe plot.
import matplotlib.pyplot as plt import numpy as np # defining variables X = np.linspace(-6, 6, 10) Y = np.linspace(-6, 6, 10) x,y = np.meshgrid(X,Y) z = np.sin(np.sqrt(x ** 2 + y ** 2)) #generating the figure and 3d axes fig = plt.figure(figsize = (6, 6)) ax = plt.axes(projection = '3d') #plotting the 3D wireframe plot ax.plot_wireframe(x, y, z) ax.contourf(x,y,z) # ax.view_init(70,60) plt.show()
Output:
The steps followed in the above examples are:
- Import required modules.
- Define the variables.
- Generate the figure and 3d axes.
- Plot the 3D wireframe plot using
Axes.plot_wireframe
method. - Plot the 2D contour plot using
Axes.contourf
method. - Change the view angle by using
Axes.view_init
method (refer this).
To understand more about Contour plots, refer this.
You might also be interested in –
- How to Create a 3D Plot in Python?
- How to plot a 3D surface plot in Python?
- How to Plot a 3D Contour plot in Python?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.