How to plot convex hull in Python?
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()
simplex
is the serial number of the index point. The effect after drawing is as follows
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()
The effect is as shown in the figure
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()
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
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], # .......
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
