# Does anyone know a way to draw lines connecting nearest neighbor points in ListPlot[ ]?

I have a two-dimensional mesh that I am visualizing in Mathematica, with ListPlot[]. The mesh looks like this: Any ideas for how to connect all the nearest neighboring points with lines?

Thanks, ahead of time.

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Nov 21 '14 at 1:32
• If the purpose is to recreate some triangulated mesh from points - you can use e.g. DelaunayMesh. – funnyp0ny Nov 21 '14 at 21:34
• Thanks! This was my purpose. I will keep this in mind in the future. I actually wound up figuring a way to visualize the mesh within the program I started using to create the mesh. "gmsh". But this will still be useful for me in the future! – wgwz Nov 22 '14 at 18:26

ListPlot[] isn't the "right" tool. It can be done with Epilog ->, but it's more natural to use Graphics[] and Nearest[]:

(*  Generate a distribution similar to your example *)
n = 1000;
rs = RandomVariate[TransformedDistribution[Sqrt@x,x\[Distributed] UniformDistribution[{.1, 1}]], n];
phis = RandomReal[{0, 2 Pi}, n];
pts = #1 {Cos@#2, Sin@#2} & @@@ Transpose[{rs, phis}];

(* Find the lines and plot them*)
p = Nearest[pts];
pts1 = p[#, 2][] & /@ pts;
Graphics[{Point@pts, Line[{##}] & @@@ Transpose[{pts, pts1}]},  Axes -> True] The standard doc for Nearest[] is not complete. This one is much better.

If you want something like "percolation style" neighbors (almost, but not exactly), you can do:

k = {};
AppendTo[k, {#, (h = p[#, 100])[[2 +
Sum[Boole@MemberQ[k, {h[[i]], #}], {i, 2, 100}]]]}] & /@ pts; 