Plotting a set of given points to form a closed curve in matplotlib

前端 未结 2 1146
广开言路
广开言路 2020-12-10 08:25

I have (tons of) coordinates of points for closed curve(s) sorted in x-increasing order.

When plot it in the regular way the result i get is this:

(circle

2条回答
  •  孤城傲影
    2020-12-10 09:26

    If you don't know how your points are set up (if you do I recommend you follow that order, it will be faster) you can use Convex Hull from scipy:

    import matplotlib.pyplot as plt
    from scipy.spatial import ConvexHull
    
    # RANDOM DATA
    x = np.random.normal(0,1,100)
    y = np.random.normal(0,1,100)
    xy = np.hstack((x[:,np.newaxis],y[:,np.newaxis]))
    
    # PERFORM CONVEX HULL
    hull = ConvexHull(xy)
    
    # PLOT THE RESULTS
    plt.scatter(x,y)
    plt.plot(x[hull.vertices], y[hull.vertices])
    plt.show()
    

    , which in the example above results is this:

    Notice this method will create a bounding box for your points.

提交回复
热议问题