Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - plot only the outermost points of a dataset

I have a set of random points arranged in a square-like shape (with rough edges) and I would like to plot only the outermost points - only the points closest to the imaginary edge of the shape (so that I can then have a clear border between multiple similar datasets that have some overlap).

Any suggestions on how can I select those points is greatly appreciated.

like image 746
mannaroth Avatar asked Dec 11 '22 03:12

mannaroth


2 Answers

You could use scipy's convex hull function, see scipy docs. The docs page gives the following example

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()
like image 192
wasserfeder Avatar answered Jan 01 '23 11:01

wasserfeder


You could compute the convex hull of your dataset. Here is a pure-Python implementation; there are third-party packages which may have better performance:

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()

enter image description here

like image 40
unutbu Avatar answered Jan 01 '23 11:01

unutbu