Voronoi图的边缘:如何从scipy.spatial.Voronoi对象获取呈点(point1,point2)形式的边缘? [英] Voronoi Diagram Edges: How to get edges in the form (point1, point2) from a scipy.spatial.Voronoi object?
问题描述
我现在花了很多时间试图从 scipy.spatial.Voronoi 图中获取边缘,但无济于事.以下是主要文档:http://docs.scipy.org/doc/scipy-dev/reference/generation/scipy.spatial.Voronoi.html
I have spent a great deal of time now trying to obtain edges from the scipy.spatial.Voronoi diagram to no avail. Here is the main documentation: http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.spatial.Voronoi.html
如果您像这样创建 Voronoi 图表:
If you create a Voronoi Diagram like so:
points = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2],
[2, 0], [2, 1], [2, 2]]) //Or feel free to use any set of points
然后您可以访问以下对象属性:
then you have access to the following object properties:
vor.regions
vor.max_bound
vor.ndim
vor.ridge_dict
vor.ridge_points
vor.ridge_vertices
vor.npoints
vor.point_region
vor.points
但是不清楚如何将这些结合起来以形成二维voronoi图的形式(point1,point2)的边?我知道边缘的存在是因为您可以执行以下操作来绘制voronoi图及其edgres和顶点:
But is unclear how to combine these to get edges in the form (point1, point2) for 2d voronoi diagrams? I know edges exist because you can plot the voronoi diagram and its edgres and vertices because you can do the following:
voronoi_plot_2d(vor)
plt.show()
其中清楚地描述了voronoi edgres-如何获取它们的列表以及它们的起点和终点?没关系,如果我只得到实心的边缘(而不是从图上无界的虚线的边缘)
which clearly depicts voronoi edgres - how to get a list of them and their starting and endpoints? Its okay if I only get the solid edges (not the dotted ones which go unbounded off the plot)
推荐答案
查看 ridge_vertices
属性:
ridge_vertices (list of list of ints, shape (nridges, *))
Indices of the Voronoi vertices forming each Voronoi ridge.
该列表中的每个元素都是一对整数.每个整数都是一个索引进入 vertices
列表.因此,每个元素都定义了一条要绘制的线Voronoi 图.索引为 -1 表示在无穷远处"的点.
Each element in that list is a pair of integers. Each integer is an index
into the vertices
list. So each element defines a line to be draw in the
Voronoi diagram. An index of -1 means a point that is "at infinity".
这是绘制 Voronoi 图线条的脚本:
Here's script that draws the lines of the Voronoi diagram:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi
points = np.array([[0, 0], [0, 1], [0, 2],
[1, 0], [1, 1], [1, 2],
[2, 0], [2, 1], [2, 2]])
vor = Voronoi(points)
fig = plt.figure()
# Mark the Voronoi vertices.
plt.plot(vor.vertices[:,0], vor.vertices[:, 1], 'ko', ms=8)
for vpair in vor.ridge_vertices:
if vpair[0] >= 0 and vpair[1] >= 0:
v0 = vor.vertices[vpair[0]]
v1 = vor.vertices[vpair[1]]
# Draw a line from v0 to v1.
plt.plot([v0[0], v1[0]], [v0[1], v1[1]], 'k', linewidth=2)
plt.show()
它创建:
这篇关于Voronoi图的边缘:如何从scipy.spatial.Voronoi对象获取呈点(point1,point2)形式的边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!