Creating 3D surface Plot
The axes3d present in Matplotlib’s mpl_toolkits.mplot3d toolkit provides the necessary functions used to create 3D surface plots.Surface plots are created by using ax.plot_surface() function.
Syntax:
ax.plot_surface(X, Y, Z)
where X and Y are 2D array of points of x and y while Z is 2D array of heights.Some more attributes of ax.plot_surface() function are listed below:
Attribute | Description |
---|---|
X, Y, Z | 2D arrays of data values |
cstride | array of column stride(step size) |
rstride | array of row stride(step size) |
ccount | number of columns to be used, default is 50 |
rcount | number of row to be used, default is 50 |
color | color of the surface |
cmap | colormap for the surface |
norm | instance to normalize values of color map |
vmin | minimum value of map |
vmax | maximum value of map |
facecolors | face color of individual surface |
shade | shades the face color |
Example: Let’s create a 3D surface by using the above function
Python3
# Import libraries from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt # Creating dataset x = np.outer(np.linspace( - 3 , 3 , 32 ), np.ones( 32 )) y = x.copy().T # transpose z = (np.sin(x * * 2 ) + np.cos(y * * 2 ) ) # Creating figure fig = plt.figure(figsize = ( 14 , 9 )) ax = plt.axes(projection = '3d' ) # Creating plot ax.plot_surface(x, y, z) # show plot plt.show() |
Output:
3D Surface plotting in Python using Matplotlib
A Surface Plot is a representation of three-dimensional dataset. It describes a functional relationship between two independent variables X and Z and a designated dependent variable Y, rather than showing the individual data points. It is a companion plot of the contour plot. It is similar to the wireframe plot, but each face of the wireframe is a filled polygon. This helps to create the topology of the surface which is being visualized.