ListPlot join points by nearest neighbor rather than order
You can use FindCurvePath
to reorder your data. However, FindCurvePath
expects the scale of the two coordinates to be close, so you need to rescale first:
new = FindCurvePath[data . {{1, 0}, {0, 100}}]
ListLinePlot[data[[#]]& /@ new]
{{2, 1, 3, 6, 9, 11, 13, 15, 17, 19, 21, 23, 26, 27, 30, 31, 32, 29, 28, 25, 24, 22, 20, 18, 16, 14, 12, 10, 8, 7, 5, 4, 2}}
Update
Roman suggested automating the scaling of the data. Here is one possibility for rescaling the data:
rescale = RescalingTransform[CoordinateBounds[data]] @ data;
Then, using FindCurvePath
on the rescaled data:
new = FindCurvePath @ rescale
{{2, 1, 3, 6, 9, 11, 13, 15, 17, 19, 21, 23, 26, 27, 30, 31, 32, 29, 28, 25, 24, 22, 20, 18, 16, 14, 12, 10, 8, 7, 5, 4, 2}}
produces the same result.
Since your data can form a star convex polygon, we can sort by the angle with respect to a certain point:
center = Mean[data];
ListLinePlot[ArrayPad[SortBy[data, ArcTan @@ (# - center) &], {{0, 1}}, "Periodic"]]
By scaling the data into the covariance ellipsoid, we can achieve hands-free auto-scaling before calculating a FindCurvePath
along @CarlWoll 's solution:
path = First@FindCurvePath[
data.Transpose[#[[2]]/Sqrt[#[[1]]]&@Eigensystem[Covariance[data]]]]
{2, 1, 3, 6, 9, 11, 13, 15, 17, 19, 21, 23, 26, 27, 30, 31, 32, 29, 28, 25, 24, 22, 20, 18, 16, 14, 12, 10, 8, 7, 5, 4, 2}
ListPlot[data[[path]]]
Alternatively, if the data points are meant to describe a closed loop, the path can be found with
path = Last@FindShortestTour[
data.Transpose[#[[2]]/Sqrt[#[[1]]]&@Eigensystem[Covariance[data]]]]
{1, 2, 4, 5, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 28, 29, 32, 31, 30, 27, 26, 23, 21, 19, 17, 15, 13, 11, 9, 6, 3, 1}
The transformed data that are fed into FindCurvePath
or FindShortestTour
have a unit covariance matrix, which makes it easier to find a good path:
Sdata = data.Transpose[#[[2]]/Sqrt[#[[1]]]&@Eigensystem[Covariance[data]]];
Chop@Covariance[Sdata]
{{1., 0}, {0, 1.}}
We can see that these scaled points nearly lie on a circle:
ListPlot[Sdata, AspectRatio -> Automatic]