bartimar
bartimar

Reputation: 3534

How to fill area between multiple points in a graph

I have an array in python with x,y points used to draw a polygon = feasible region.

[[0.0, 10.0], [0.0, 20.0], [10.0, 0.0], [20.0, 0.0]]

The problem is, I need the biggest area between the points to be filled. So I need to reorder the array so I'd have the proper order to draw the biggest polygon possible. The order is important as it connects the neighbour points in the array. I tried to sort the array by first element ascending, the second descending, but nothing seems to work.

def scatterplot(points):

 points.sort(key=operator.itemgetter(0,1))

 x1 = []
 y1 = []

 for p in range(0,len(points)):
    plt.plot(points[p][0],points[p][1],'go',markersize=10)
    x1.append(points[p][0])
    y1.append(points[p][2])


 plt.fill(x1,y1,'red',alpha=1)
 plt.show()

I guess I'm missing something from math here. Any help will be appreciated. Thanks

[[0.0, 10.0], [0.0, 20.0], [10.0, 0.0], [20.0, 0.0]]

enter image description here

[[0.0, -0.0], [0.0, 4.0], [2.0, 0.0], [1.5, 2.0]]

enter image description here

Upvotes: 1

Views: 3474

Answers (2)

abhra
abhra

Reputation: 194

Here you go.

import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt

pts1 = [[0.0, 10.0], [0.0, 20.0], [10.0, 0.0], [20.0, 0.0]]
pts = np.array(pts1)

hull = ConvexHull(pts)

plt.fill(pts[hull.vertices,0], pts[hull.vertices,1],'red',alpha=0.5)
plt.show()

Upvotes: 2

6502
6502

Reputation: 114559

Seems you're looking for the convex hull of the points, or possibly some generalization of it like alpha-shapes.

Upvotes: 3

Related Questions