1
$\begingroup$

This question already has an answer here:

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

$\endgroup$

marked as duplicate by Kuba plotting Nov 26 '18 at 22:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ FindShortestTour? $\endgroup$ – That Gravity Guy Nov 26 '18 at 21:53
  • $\begingroup$ Seems to work a treat -- thank you! Didn't know of that function. $\endgroup$ – ben18785 Nov 26 '18 at 21:55
3
$\begingroup$

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

$\endgroup$
3
$\begingroup$

As suggested by That Gravity Guy, the following works,

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

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.