# How to draw lines connecting nearby points [duplicate]

Suppose I have the following example data,

data = Table[{Cos[theta], Sin[theta]}, {theta, 0, 2 Pi, Pi/8}];


which, when plotted, looks like this,

ListLinePlot[data, PlotMarkers -> {Automatic, 10}]


Instead, imagine rather than being in order, the data order is scrambled,

data = RandomSample[data, Length@data];


Now, the plot looks not too pretty,

ListLinePlot[data, PlotMarkers -> {Automatic, 10}]


Is there a simple way to draw straight lines (not curves) connecting the points as in the top graph, using the scrambled data (when we do not know the true order)?

• FindShortestTour? – NonDairyNeutrino Nov 26 '18 at 21:53
• Seems to work a treat -- thank you! Didn't know of that function. – ben18785 Nov 26 '18 at 21:55

You can also use ListCurvePathPlot:

ListCurvePathPlot[data,
Mesh -> All, MeshStyle -> Directive[ColorData[97][1], PointSize[Large]]]


Alternatively, use FindCurvePath and ListLinePlot:

ListLinePlot[data[[FindCurvePath[data][[1]]]], Mesh -> All]


As suggested by That Gravity Guy, the following works,

ListLinePlot[data[[FindShortestTour[data][[2]]]], PlotMarkers -> {Automatic, 10}]