[Python] How to fill area between multiple points in a graph

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    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]]


    [​IMG]

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


    [​IMG]

    Continue reading...

Compartilhe esta Página