计算和显示凸包

问题描述 投票:0回答:1

我正在尝试为python中的一些随机点计算并显示凸包。

这是我当前的代码:

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

points = np.random.rand(15,2)   #Random points in 2-D

hull = ConvexHull(points)

plt.plot(points[:,0], points[:,1], 'o',color='k')

for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1], 'c')

plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'c', lw=-1)
plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'c-')
plt.show()

我的问题:

  • 如何更改X,Y标签和点数限制在0到9之间?例如(0,1,2,3,4,5,6,7,8,9)
  • 如何在凸包上用圆标记点?As in example
python matplotlib scipy convex-hull
1个回答
0
投票

np.rand()替换randint(0, 10)将生成从0,1,...9的整数形式的坐标。

使用'。'因为标记将导致给定点的标记变小。

使用'o'作为标记,设置markeredgeedgecolor并将主色设置为'none'将产生一个圆形标记,该标记可用于船体上的点。标记的大小可以通过markersize=进行调整。

((我不理解plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'c-'),所以我省略了。)

下面是一些示例代码:

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

points = np.random.randint(0, 10, size=(15,2))   #Random points in 2-D

hull = ConvexHull(points)

plt.plot(points[:,0], points[:,1], '.',color='k')

for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1], 'c')

plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'o', mec='r', color='none', lw=1, markersize=10)
plt.xticks(range(10))
plt.yticks(range(10))
plt.show()

resulting plot

© www.soinside.com 2019 - 2024. All rights reserved.