Skip to content
Related Articles

Related Articles

Improve Article

Tri-Surface Plot in Python using Matplotlib

  • Last Updated : 10 May, 2020
Geek Week

A Tri-Surface Plot is a type of surface plot, created by triangulation of compact surfaces of finite number of triangles which cover the whole surface in a manner that each and every point on the surface is in triangle. The intersection of any two triangles results in void or a common edge or vertex. This type of plot is created where the evenly sampled grids are restrictive and inconvenient to plot. Generally Tri-Surface plots are created by calling ax.plot_trisurf() function of matplotlib library. Some of the attributes of the function are listed below:

X, Y, Zdataset as 1D array to be plotted
colorscolor of the surface patches
cmapcolor map to set the color of surface patches
normparameter to normalize map values of colors
vminminimum value of map
vamxmaximum value of map
shadeattribute to shade the facecolors

Example 1 : Let’s create a basic Tri-Surface plot using the ax.plot_trisurf() function.

# Import libraries
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
# Creating dataset
z = np.linspace(0, 50000, 100)
x = np.sin(z)
y = np.cos(z) 
# Creating figyre
fig = plt.figure(figsize =(14, 9))
ax = plt.axes(projection ='3d')
# Creating plot
ax.plot_trisurf(x, y, z,
                linewidth = 0.2,
                antialiased = True);
# show plot

Output :
Tri-Surface Plot in Python using Matplotlib

Example 2 : For better understanding Let’s take another example.

# Import libraries
from mpl_toolkits.mplot3d import Axes3D  
import matplotlib.pyplot as plt  
import numpy as np  
# Creating radii and angles
r = np.linspace(0.125, 1.0, 100)  
a = np.linspace(0, 2 * np.pi, 
                endpoint = False)  
# Repeating all angles for every radius  
a = np.repeat(a[..., np.newaxis], 100, axis = 1)  
# Creating datset
x = np.append(0, (r * np.cos(a)))  
y = np.append(0, (r * np.sin(a)))    
z = (np.sin(x ** 4) + np.cos(y ** 4)) 
# Creating figure
fig = plt.figure(figsize =(16, 9))  
ax = plt.axes(projection ='3d')  
# Creating color map
my_cmap = plt.get_cmap('hot')
# Creating plot
trisurf = ax.plot_trisurf(x, y, z,
                         cmap = my_cmap,
                         linewidth = 0.2
                         antialiased = True,
                         edgecolor = 'grey')  
fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 5)
ax.set_title('Tri-Surface plot')
# Adding labels
ax.set_xlabel('X-axis', fontweight ='bold'
ax.set_ylabel('Y-axis', fontweight ='bold'
ax.set_zlabel('Z-axis', fontweight ='bold')
# show plot

Output :
Tri-Surface Plot in Python using Matplotlib

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :