Home > Backend Development > Python Tutorial > How to plot convex hull in Python?

How to plot convex hull in Python?

WBOY
Release: 2023-05-08 19:25:06
forward
1966 people have browsed it

ConvexHull

ConvexHull is a class in spatial. Its main function is to find the edges of a set of points and make a convex hull. The necessary initialization parameter is a point set, the point set format is an array of n×m dimensions, n is the number of points in the point set, and m is the dimension of the point.

from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
import numpy as np

pts = np.random.rand(30, 2)
hull = ConvexHull(pts)
plt.plot(pts[:,0], pts[:,1], 'o')
for i in hull.simplices:
    plt.plot(pts[i, 0], pts[i, 1], 'k-')

plt.show()
Copy after login

simplex is the serial number of the index point. The effect after drawing is as follows

How to plot convex hull in Python?

ConvexHull has two optional parameters, among which, incremental is a Boolean parameter. When it is True, new points are allowed to be added.

The specific parameters of qhull_options can be viewed in qhull. Only QG will be demonstrated below.

QG

QGn means treating the nth point as an observation point. After dividing the point set by convex hull, if the vertices are connected and used as a wall, then the observation point can The points that can be seen are marked as good, and the effect is as follows

pts = np.random.rand(1000, 2)
# 添加一个观察点
pts = np.vstack([pts, np.array([[2,0.5]])])
hull = ConvexHull(pts, qhull_options='QG1000')
plt.plot(pts[:,0], pts[:,1], '.')
for i in hull.simplices:
    plt.plot(pts[i, 0], pts[i, 1], 'k-')

for i in hull.simplices[hull.good]:
    plt.plot(pts[i, 0],pts[i, 1], lw=5)

plt.show()
Copy after login

The effect is as shown in the figure

How to plot convex hull in Python?

Three-dimensional situation

The convex hull in two dimensions is obviously a closed figure composed of lines, while the convex hull in three dimensions should naturally be a three-dimensional geometry. Expanded to any dimension, the convex hull is actually a simplex. The simplices in ConvexHull are the points that form the simplex and are indexed in the origin set. An example is as follows

pts = np.random.rand(30, 3)
hull = ConvexHull(pts)
ax = plt.subplot(projection='3d')
ax.scatter(pts[:,0], pts[:,1], pts[:,2])
for i in hull.simplices:
    ax.plot_trisurf(pts[i, 0], pts[i, 1], pts[i,2], alpha=0.5)

plt.show()
Copy after login

The alpha parameter is used to adjust the transparency of the triangular surface, so that the points inside the convex hull can be seen through the convex hull.

The effect is as follows

How to plot convex hull in Python?

ConvexHull attribute

The concept of a simplex has been introduced before, that is, the figure composed of the convex hull is a simplex . As a two-dimensional case, the convex hull is surrounded by line segments; in the three-dimensional case, the convex hull is surrounded by planes; extended to any dimension, it can be expressed as a simplex that constitutes the convex hull, surrounded by hypersurfaces. Since the concept of hypersurface has no boundaries, the convex hull surface with vertices and edges is referred to as a simplex hypersurface in the following.

The commonly used attributes in the ConvexHull class are as follows

  • points Point set surrounded by convex hull

  • vertices Simplex vertices at points Concentrated indexes

  • simplices Simplex metasurface vertices

  • neighbors Indexes of hypersurface adjacent hypersurfaces

  • equations Parameters of hypersurface equations

Examples of hypersurface equations in three dimensions are as follows, that is, each hypersurface has 4 parameters

>>> hull.equations
array([[-0.5509472 ,  0.72386104, -0.41530999, -0.36369123],
       [-0.26155355,  0.16210178, -0.95147925,  0.02022163],
       [-0.99132368, -0.0460725 ,  0.12310441,  0.045523  ],
       [-0.98526526, -0.07170442,  0.15527666,  0.04749854],
       [-0.15900968, -0.98529789, -0.06248198,  0.13294496],
   # .......
Copy after login

The above is the detailed content of How to plot convex hull in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template