Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-仅绘制数据集的最外层点_Python_Matplotlib_Plot - Fatal编程技术网

Python-仅绘制数据集的最外层点

Python-仅绘制数据集的最外层点,python,matplotlib,plot,Python,Matplotlib,Plot,我有一组排列成方形的随机点(带有粗糙边缘),我只想绘制最外面的点——只绘制最靠近形状假想边缘的点(这样我就可以在多个有重叠的类似数据集之间有一个清晰的边界) 非常感谢您对我如何选择这些要点的任何建议 您可以计算数据集的凸包。这里有一个;有几种可能具有更好的性能: import random import sys import matplotlib.pyplot as plt CLOCKWISE = -1 COLLINEAR = 0 COUNTERCLOCKWISE = +1 eps = sys

我有一组排列成方形的随机点(带有粗糙边缘),我只想绘制最外面的点——只绘制最靠近形状假想边缘的点(这样我就可以在多个有重叠的类似数据集之间有一个清晰的边界)


非常感谢您对我如何选择这些要点的任何建议

您可以计算数据集的凸包。这里有一个;有几种可能具有更好的性能:

import random
import sys
import matplotlib.pyplot as plt

CLOCKWISE = -1
COLLINEAR = 0
COUNTERCLOCKWISE = +1
eps = sys.float_info.epsilon


def orientation(a, b):
    x0, y0 = a
    x1, y1 = b
    cross = x0 * y1 - x1 * y0
    if cross > eps:
        return COUNTERCLOCKWISE
    elif cross < -eps:
        return CLOCKWISE
    else:
        return COLLINEAR


def same_halfplane(a, b):
    x0, y0 = a
    x1, y1 = b
    dot = x0 * x1 + y0 * y1
    if dot >= eps:
        return True
    elif dot < eps:
        return False


def jarvis(points):
    """
    http://cgi.di.uoa.gr/~compgeom/pycgalvisual/whypython.shtml
    Jarvis Convex Hull algorithm.
    """
    points = points[:]
    r0 = min(points)
    hull = [r0]
    r, u = r0, None
    remainingPoints = [x for x in points if x not in hull]
    while u != r0 and remainingPoints:
        u = random.choice(remainingPoints)
        for t in points:
            a = (u[0] - r[0], u[1] - r[1])
            b = (t[0] - u[0], t[1] - u[1])
            if (t != u and
                (orientation(a, b) == CLOCKWISE or
                 (orientation(a, b) == COLLINEAR and
                  same_halfplane(a, b)))):
                u = t
        r = u
        points.remove(r)
        hull.append(r)
        try:
            remainingPoints.remove(r)
        except ValueError:
            # ValueError: list.remove(x): x not in list
            pass
    return hull

if __name__ == '__main__':
    points = iter(random.uniform(0, 10) for _ in xrange(20))
    points = zip(points, points)
    hull = jarvis(points)
    px, py = zip(*points)
    hx, hy = zip(*hull)
    plt.plot(px, py, 'b.', markersize=10)
    plt.plot(hx, hy, 'g.-', markersize=10)
    plt.show()
随机导入
导入系统
将matplotlib.pyplot作为plt导入
顺时针=-1
共线=0
逆时针=+1
eps=sys.float\u info.epsilon
def方向(a、b):
x0,y0=a
x1,y1=b
交叉=x0*y1-x1*y0
如果交叉>每股收益:
逆时针返回
elif交叉<-eps:
顺时针返回
其他:
返回共线
def相同_半平面(a、b):
x0,y0=a
x1,y1=b
点=x0*x1+y0*y1
如果dot>=eps:
返回真值
elif dot

您可以使用scipy的凸包函数,请参阅。 文档页面给出了以下示例

from scipy.spatial import ConvexHull
points = np.random.rand(30, 2)   # 30 random points in 2-D
hull = ConvexHull(points)

import matplotlib.pyplot as plt
plt.plot(points[:,0], points[:,1], 'o')
# plot convex hull polygon
plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r--', lw=2)
# plot convex full vertices
plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'ro')
plt.show()

你能提供到目前为止你在am中得到的代码吗?我现在只是添加代码,但下面的答案解决了问题。谢谢