If you don't want intersections, one way to achieve this is to order your co-ordinate pairs after some rotation rule. In the example above, I first define a center point (here just the average of all x- and y-values, respectively) and then compute the angle that each co-ordinate pair defines with that center point. As JRG already said, you get a closed polygon by appending the first point to your sequence of points:
import numpy as np
from matplotlib import pyplot as plt
def draw_polygon(ax, n):
x = np.random.randint(0,50,n)
y = np.random.randint(0,50,n)
##computing the (or a) 'center point' of the polygon
center_point = [np.sum(x)/n, np.sum(y)/n]
angles = np.arctan2(x-center_point[0],y-center_point[1])
##sorting the points:
sort_tups = sorted([(i,j,k) for i,j,k in zip(x,y,angles)], key = lambda t: t[2])
##making sure that there are no duplicates:
if len(sort_tups) != len(set(sort_tups)):
raise Exception('two equal coordinates -- exiting')
x,y,angles = zip(*sort_tups)
x = list(x)
y = list(y)
##appending first coordinate values to lists:
x.append(x[0])
y.append(y[0])
ax.plot(x,y, label = '{}'.format(n))
if __name__ == '__main__':
fig,ax = plt.subplots()
for n in range(3,11,2):
draw_polygon(ax,n)
ax.legend()
plt.show()
The result looks something like this:
