1

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

enter image description here

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

enter image description here

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)?

ben18785
  • 3,167
  • 15
  • 28

2 Answers2

4

As suggested by That Gravity Guy, the following works,

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

enter image description here

ben18785
  • 3,167
  • 15
  • 28
3

You can also use ListCurvePathPlot:

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

enter image description here

Alternatively, use FindCurvePath and ListLinePlot:

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

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896