Triangulations Using Matplotlib
Last Updated :
26 Apr, 2025
Delaunay Triangulation is a fundamental concept in computational geometry, which is used to create a triangulation of a set of points in a 2D or 3D space. This process is important in various fields such as computer graphics, image processing, and numerical simulation. In this article, we will explore how to perform Delaunay Triangulation of points from a 2D surface in 3D using Python.
In this article, we will use the Python library scipy for performing Delaunay Triangulation. We will first generate a set of random points on a 2D surface and then use the scipy library to create a triangulation of these points. Finally, we will use the library matplotlib to visualize the triangulation in 3D.
Key Concepts
Delaunay Triangulation
Delaunay Triangulation is a method for creating a triangulation of a set of points in a 2D or 3D space. The key idea behind this method is that the triangles in the triangulation should not have any points inside the circumcircle of the triangle. This ensures that the triangles have a relatively uniform shape and size, which is important in many applications. Some examples of these applications are computer graphics, image processing, and numerical simulation.
In computational geometry, there are many different types of triangulations for a set of points, Delaunay triangulation is one of them, and is known for its “best” quality triangulation comparing to other triangulation methods.
Scipy
Scipy is a Python library for scientific and technical computing. It provides a wide range of functionality, including optimization, interpolation, integration, and signal and image processing. It also provides the ability to perform Delaunay triangulation of a set of points.
Matplotlib
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. It can be used to create a wide range of visualizations, including 2D and 3D plots, bar charts, scatter plots, and more. Matplotlib is a powerful library, it’s widely used and makes it easy to create beautiful visualizations. We use it here to plot the triangulation in 3D, using the plot_trisurf function, and to display it, using the show function.
Methods Used
- matplotlib.tri.Triangulation(): A class for creating a triangulation in matplotlib.
- matplotlib.pyplot.triplot(): A function for creating a 2D plot of a triangulation.
- matplotlib.pyplot.show(): A function for displaying the plots in a window.
- scipy.spatial.Delaunay(): A function that takes a set of points and returns the Delaunay triangulation of those points.
Procedure
1. Import the necessary libraries
The first step is to import the necessary libraries. We will need the NumPy, Matplotlib, and SciPy libraries for this task.
Python3
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
|
2. Obtain the 2D points dataset
Obtain the dataset of 2D points, here, we shall generate a random dataset, using numpy’s random method.
Python3
points = np.random.rand( 177 , 2 )
|
3. Perform Delaunay triangulation
Now, we can use the Delaunay class from the SciPy library to perform the triangulation.
4. Visualize the triangulation
Finally, we can use Matplotlib’s triplot function to visualize the triangulation.
Python3
plt.triplot(points[:, 0 ], points[:, 1 ], tri.simplices.copy())
plt.plot(points[:, 0 ], points[:, 1 ], 'o' )
plt.show()
|
The provided program generates the following output:
.png)
Output
This procedure may be applied as per the following examples:
Example 1:
This code uses the Python libraries numpy, matplotlib, and scipy to perform a Delaunay triangulation on a set of 2D points and visualize the resulting triangulation. The code starts by importing the necessary libraries, numpy, matplotlib, and scipy. Then it defines a 2D array of points, which represents a closed trapezoid shape in this example, that is used as the input for the Delaunay triangulation. The scipy library’s Delaunay() function is then used to perform the triangulation of the points, it takes the 2D array of points as input and returns a Delaunay triangulation object. The matplotlib library is then used to visualize the triangulation, the triplot() function creates a 2D plot of the triangulation, where the first two arguments are the x and y coordinates of the points, respectively, and the third argument is the set of triangles represented as indices of the input points. Additionally, a scatter plot of the points is created using the plt.plot(points[:,0], points[:,1], ‘o’) and it is displayed using the plt.show() function.
Python3
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
points = np.array([[ 3.0 , 0.0 ], [ 2.0 , 0.0 ], [ 2.0 , 0.75 ],
[ 2.5 , 0.75 ], [ 2.5 , 0.6 ], [ 2.25 , 0.6 ],
[ 2.25 , 0.2 ], [ 3.0 , 0.2 ], [ 3.0 , 0.0 ]])
tri = Delaunay(points)
plt.triplot(points[:, 0 ], points[:, 1 ], tri.simplices.copy())
plt.plot(points[:, 0 ], points[:, 1 ], 'o' )
plt.show()
|
Output:
Example 2:
This code uses the Python libraries numpy, matplotlib, and scipy to perform a Delaunay triangulation on a set of 2D points and visualize the resulting triangulation. It starts by importing the necessary libraries, then it creates a 2D array of points (15 points in this case) with x and y coordinates, it uses this array as input to perform the Delaunay triangulation using scipy’s Delaunay function, which returns a triangulation object. Next, it visualizes the triangulation using matplotlib’s triplot function, which creates a 2D plot of the triangulation, It also creates a scatter plot of the points using plt.plot and finally, it shows the plot using plt.show().
Python3
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
points = np.array([[ 0 , 0 ], [ 0 , 1 ],
[ 0 , 2 ], [ 1 , 0 ], [ 1 , 1 ],
[ 1 , 2 ], [ 2 , 0 ], [ 2 , 1 ],
[ 2 , 2 ], [ 3 , 0 ], [ 3 , 1 ],
[ 3 , 2 ], [ 4 , 0 ], [ 4 , 1 ],
[ 4 , 2 ]])
tri = Delaunay(points)
plt.triplot(points[:, 0 ], points[:, 1 ], tri.simplices.copy())
plt.plot(points[:, 0 ], points[:, 1 ], 'o' )
plt.show()
|
Output:
Example 3:
This code uses the Python libraries numpy, matplotlib, and scipy to perform a Delaunay triangulation on a set of 2D points and visualize the resulting triangulation. It starts by importing the necessary libraries, then it creates a 2D array of points (9 points in this case) with x and y coordinates, it uses this array as input to perform the Delaunay triangulation using scipy’s Delaunay function, which returns a triangulation object. Next, it visualizes the triangulation using matplotlib’s triplot function, which creates a 2D plot of the triangulation, It also creates a scatter plot of the points using plt.plot and finally, it shows the plot using plt.show().
Python3
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
points = np.array([[ 0 , 0 ], [ 0 , 1 ],
[ 0 , 2 ], [ 1 , 0 ],
[ 1 , 1 ], [ 1 , 2 ],
[ 2 , 0 ], [ 2 , 1 ],
[ 2 , 2 ]])
tri = Delaunay(points)
plt.triplot(points[:, 0 ], points[:, 1 ], tri.simplices.copy())
plt.plot(points[:, 0 ], points[:, 1 ], 'o' )
plt.show()
|
Output:
In conclusion, Delaunay triangulation is a powerful mathematical concept that can be used to triangulate a set of points in a plane. In this article, we discussed how to perform Delaunay triangulation of points from a 2D surface in 3D using Python. We covered the steps required to generate some random points, perform the triangulation, and visualize the resulting triangulation. We also provided a few examples of Delaunay triangulation using different sets of points, and demonstrated how to generate a set of points on a sphere that can be triangulated to yield a 3D sphere.
Similar Reads
Multiplots in Python using Matplotlib
Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
3 min read
3D Scatter Plotting in Python using Matplotlib
A 3D Scatter Plot is a mathematical diagram that visualizes data points in three dimensions, allowing us to observe relationships between three variables of a dataset. Matplotlib provides a built-in toolkit called mplot3d, which enables three-dimensional plotting. To create a 3D Scatter Plot, we use
4 min read
Simple Plot in Python using Matplotlib
Matplotlib is a Python library that helps in visualizing and analyzing the data and helps in better understanding of the data with the help of graphical, pictorial visualizations that can be simulated using the matplotlib library. Matplotlib is a comprehensive library for static, animated and intera
5 min read
Matplotlib Tutorial
Matplotlib is an open-source visualization library for the Python programming language, widely used for creating static, animated and interactive plots. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, Qt, GTK and wxPython. It
5 min read
Save Plot To Numpy Array using Matplotlib
Saving a plot to a NumPy array in Python is a technique that bridges data visualization with array manipulation allowing for the direct storage of graphical plots as array representations, facilitating further computational analyses or modifications within a Python environment. Let's learn how to Sa
4 min read
3D Sine Wave Using Matplotlib - Python
Prerequisites: Mathplotlib, NumPy In the Cartesian coordinate system, the trigonometric sine function sin(x) sin (x) generates a regular undulating curve, which passes through the origin. Its values range between â1 â 1 and 1 1 for all real values of x x.In this article, we will plot a sin wave grap
2 min read
Contour Plot using Matplotlib - Python
Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables we want to plot then the response Z will be plotted as slices on the X-Y plane due to which contours are sometimes referred as Z-slices or
2 min read
Tri-Surface Plot in Python using Matplotlib
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
2 min read
3D Contour Plotting in Python using Matplotlib
Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit. Let's look a
2 min read
How to resize Matplotlib RadioButtons?
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Radio buttons are the most commonly used widgets in visualization. Radio buttons let us
3 min read