Ata*_*ADA 7 python matplotlib scipy convex-hull
我正在尝试计算并显示 python 中一些随机点的凸包。
这是我当前的代码:
import numpy as np
import random
import matplotlib.pyplot as plt
import cv2
points = np.random.rand(25,2)
hull = ConvexHull(points)
plt.plot(points[:,0], points[:,1], 'o',color='c')
for simplex in hull.simplices:
plt.plot(points[simplex, 0], points[simplex, 1], 'r')
plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r', lw=-1)
plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'r-')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我的问题:
Joh*_*anC 10
替换np.rand()为将生成从到 的randint(0, 10)整数坐标。0,1,...9
使用 '。' 作为标记将导致给定点的标记更小。
使用“o”作为标记,设置标记边缘颜色并将主颜色设置为“无”将产生一个圆形标记,可用于船体上的点。标记的大小可以通过 进行调整markersize=。
fig, axes = plt.subplots(ncols=..., nrows=...)是创建多个子图的便捷方法。
这是一个最小示例的一些代码:
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)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 3))
for ax in (ax1, ax2):
ax.plot(points[:, 0], points[:, 1], '.', color='k')
if ax == ax1:
ax.set_title('Given points')
else:
ax.set_title('Convex hull')
for simplex in hull.simplices:
ax.plot(points[simplex, 0], points[simplex, 1], 'c')
ax.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10)
ax.set_xticks(range(10))
ax.set_yticks(range(10))
plt.show()
Run Code Online (Sandbox Code Playgroud)
PS:要在单独的窗口中显示绘图:
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)
for plot_id in (1, 2):
fig, ax = plt.subplots(ncols=1, figsize=(5, 3))
ax.plot(points[:, 0], points[:, 1], '.', color='k')
if plot_id == 1:
ax.set_title('Given points')
else:
ax.set_title('Convex hull')
for simplex in hull.simplices:
ax.plot(points[simplex, 0], points[simplex, 1], 'c')
ax.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10)
ax.set_xticks(range(10))
ax.set_yticks(range(10))
plt.show()
Run Code Online (Sandbox Code Playgroud)