python scipy Delaunay plotting point cloud

2024/10/3 2:16:24

I have a pointlist=[p1,p2,p3...] where p1 = [x1,y1],p2=[x2,y2] ...

I want to use scipy.spatial.Delaunay to do trianglation on these point clouds and then plot it

How can i do this ?

The documentation for the Delaunay is really scarce

so far i have this code

from subprocess import Popen, PIPE
import osos.environ['point_num'] = "2000"cmd = 'rbox $point_num D2 | tail -n $point_num'
sub_process = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
output = sub_process.communicate()
points = [line.split() for line in output[0].split('\n') if line]
x = [p[0] for p in points if p]
y = [p[1] for p in points if p]import matplotlib.pyplot as plt
plt.plot(x,y,'bo')from scipy.spatial import Delaunaydl = Delaunay(points)
convex = dl.convex_hullfrom numpy.core.numeric import reshape,shape
convex = reshape(convex,(shape(convex)[0]*shape(convex)[1],1))
convex_x = [x[i] for i in convex]
convex_y = [y[i] for i in convex]plt.plot(convex_x,convex_y,'r')
plt.show()

Thanks

Answer

EDIT: plot also the convex hull

import numpy as np
from scipy.spatial import Delaunaypoints = np.random.rand(30, 2) # 30 points in 2-d
tri = Delaunay(points)# Make a list of line segments: 
# edge_points = [ ((x1_1, y1_1), (x2_1, y2_1)),
#                 ((x1_2, y1_2), (x2_2, y2_2)),
#                 ... ]
edge_points = []
edges = set()def add_edge(i, j):"""Add a line between the i-th and j-th points, if not in the list already"""if (i, j) in edges or (j, i) in edges:# already addedreturnedges.add( (i, j) )edge_points.append(points[ [i, j] ])# loop over triangles: 
# ia, ib, ic = indices of corner points of the triangle
for ia, ib, ic in tri.vertices:add_edge(ia, ib)add_edge(ib, ic)add_edge(ic, ia)# plot it: the LineCollection is just a (maybe) faster way to plot lots of
# lines at once
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollectionlines = LineCollection(edge_points)
plt.figure()
plt.title('Delaunay triangulation')
plt.gca().add_collection(lines)
plt.plot(points[:,0], points[:,1], 'o', hold=1)
plt.xlim(-1, 2)
plt.ylim(-1, 2)# -- the same stuff for the convex hulledges = set()
edge_points = []for ia, ib in tri.convex_hull:add_edge(ia, ib)lines = LineCollection(edge_points)
plt.figure()
plt.title('Convex hull')
plt.gca().add_collection(lines)
plt.plot(points[:,0], points[:,1], 'o', hold=1)
plt.xlim(-1, 2)
plt.ylim(-1, 2)
plt.show()

Note that using scipy.spatial.Delaunay just for computing the complex hull is probably overkill, because computing just the hull can in principle done faster than computing the triangulation. Unfortunately, there's no interface in Scipy yet for computing hulls directly with Qhull.

https://en.xdnf.cn/q/70774.html

Related Q&A

Pythonic way to verify parameter is a sequence but not string

I have a function that gets a list of DB tables as parameter, and returns a command string to be executed on these tables, e.g.:pg_dump( file=/tmp/dump.sql,tables=(stack, overflow),port=5434name=europe…

How to get a random (bootstrap) sample from pandas multiindex

Im trying to create a bootstrapped sample from a multiindex dataframe in Pandas. Below is some code to generate the kind of data I need.from itertools import product import pandas as pd import numpy a…

Python Regex - replace a string not located between two specific words

Given a string, I need to replace a substring with another in an area not located between two given words.For example:substring: "ate" replace to "drank", 1st word - "wolf"…

Vectorized Lookups of Pandas Series to a Dictionary

Problem Statement:A pandas dataframe column series, same_group needs to be created from booleans according to the values of two existing columns, row and col. The row needs to show True if both cells …

Why cant I get my static dir to work with django 1.3?

This problem is very simple, but I just cant figure it outadded to my urlpatternsurl(r^static/(?P<path>.*)$, django.views.static.serve, {document_root: /home/user/www/site/static})where my main.…

Desktop Launcher for Python Script Starts Program in Wrong Path

I can not launch a python script from a .desktop launcher created on Linux Mint 17.1 Cinnamon.The problem is that the script will be launched in the wrong path - namely the home folder instead of the d…

How does numpy broadcasting perform faster?

In the following question, https://stackoverflow.com/a/40056135/5714445Numpys broadcasting provides a solution thats almost 6x faster than using np.setdiff1d() paired with np.view(). How does it manage…

python check if utf-8 string is uppercase

I am having trouble with .isupper() when I have a utf-8 encoded string. I have a lot of text files I am converting to xml. While the text is very variable the format is static. words in all caps should…

Failed building wheel for Twisted in Windows 10 python 3

Im trying to install rasa-core on my windows 10 machine.When installing with pip install, I get: Failed building wheel for TwistedThe same error appears when trying to install Twisted separately.How co…

How to set a fill color between two vertical lines

Using matplotlib, we can "trivially" fill the area between two vertical lines using fill_between() as in the example: https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/fill_between_…