3d wireframe plot in python using matplotlib

How to Plot a 3D Wireframe Plot in Python?

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 resulting wireframe plot

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 resulting 3d wireframe plot

The steps followed in the above examples are:

  • Import required modules.
  • Define the variables. Here we used mplot3d from the mpl_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 resulting 3d wireframe plot with orange color

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 the edgecolor.

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 resulting 3d wireframe plot with a different viewing angle

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 resulting 3d wireframe plot with a contour plot at the base

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.

Scroll to Top