我正在尝试为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()
我的问题:
用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()