Matplotlib 3D Surface Plot – plot_surface() Function

In Matplotlib’s mpl_toolkits.mplot3d toolkit there is axes3d present that provides the necessary functions that are very useful in creating 3D surface plots.

The representation of a three-dimensional dataset is mainly termed as the Surface Plot.

  • One thing is important to note that the surface plot provides a relationship between two independent variables that are X and Z and a designated dependent variable that is Y, rather than just showing the individual data points.
  • The Surface plot is a companion plot to the Contour Plot and it is similar to wireframe plot but there is a difference too and it is each wireframe is basically a filled polygon.
  • With the help of this, the topology of the surface can be visualized very easily.

Creation of 3D Surface Plot

To create the 3-dimensional surface plot the ax.plot_surface() function is used in matplotlib.

The required syntax for this function is given below:

ax.plot_surface(X, Y, Z)

In the above syntax, the X and Y mainly indicate a 2D array of points x and y while Z is used to indicate the 2D array of heights.

plot_surface() Attributes

Some attributes of this function are as given below:

1. shade

This attribute is used to shade the face color.

2. facecolors

This attribute is used to indicate the face color of the individual surface

3. vmax

This attribute indicates the maximum value of the map.

4. vmin

This attribute indicates the minimum value of the map.

5. norm

This attribute acts as an instance to normalize the values of color map

6. color

This attribute indicates the color of the surface

7. cmap

This attribute indicates the colormap of the surface

8. rcount

This attribute is used to indicate the number of rows to be used The default value of this attribute is 50

9. ccount

This attribute is used to indicate the number of columns to be used The default value of this attribute is 50

10. rstride

This attribute is used to indicate the array of row stride(that is step size)

11. cstride

This attribute is used to indicate the array of column stride(that is step size)

3D Surface Plot Basic Example

Below we have a code where we will use the above-mentioned function to create a 3D Surface Plot:

from mpl_toolkits import mplot3d 
import numpy as np 
import matplotlib.pyplot as plt 

x = np.outer(np.linspace(-4, 4, 33), np.ones(33)) 
y = x.copy().T
z = (np.sin(x **2) + np.cos(y **2) ) 


fig = plt.figure(figsize =(14, 9)) 
ax = plt.axes(projection ='3d') 

ax.plot_surface(x, y, z) 


plt.show() 

The output for the above code is as follows:

3D surface plot matplotlib basic example

Gradient Surface Plot

This plot is a combination of a 3D surface plot with a 2D contour plot. In the Gradient surface plot, the 3D surface is colored same as the 2D contour plot. The parts that are high on the surface contains different color rather than the parts which are low at the surface.

The required syntax is:

ax.plot_surface(X, Y, Z, cmap, linewidth, antialiased)

where cmap is used to set the color for the surface.

Gradient Surface Plot Example

Now it’s time to cover a gradient surface plot. The code snippet for the same is given below:

from mpl_toolkits import mplot3d 
import numpy as np 
import matplotlib.pyplot as plt 

x = np.outer(np.linspace(-3, 3, 32), np.ones(32)) 
y = x.copy().T 
z = (np.sin(x **2) + np.cos(y **2) ) 

fig = plt.figure(figsize =(14, 9)) 
ax = plt.axes(projection ='3d') 

my_cmap = plt.get_cmap('cool') 

surf = ax.plot_surface(x, y, z, cmap = my_cmap, edgecolor ='none') 

fig.colorbar(surf, ax = ax, shrink = 0.7, aspect = 7) 

ax.set_title('Surface plot') 


plt.show() 

The output for the above code is as follows:

gradient surface plot matplotlib basic example
Follow Us On