2
$\begingroup$

I have a list of some points. I plotted each point and its closest surrounding points.

points = Import[
   "/Users/Desktop/100nm12.txt", "Data"];
data = Table[{points[[i]][[1]], points[[i]][[2]]}, {i, 1, Length[points]}]

(*data here is imported in form of x,y coordinates*)
(*I am going to skip the part where I calculate position of each particle and its neighbors.*) 

pPar[i_] := 
 ListPlot[{data[[i]]}, AspectRatio -> Automatic, 
  PlotStyle -> Directive[PointSize[0.1], Black], 
  PlotRange -> {{760, 830}, {0, 700}}]

pNeighbs2[i_] := 
 ListPlot[Table[data[[listy[i][[j]]]], {j, 1, Length[listy[i]]}], 
  AspectRatio -> Automatic, 
  PlotStyle -> Directive[PointSize[0.1], Red], 
  PlotRange -> {{data[[i]][[1]] - 20, 
     data[[i]][[1]] + 20}, {data[[i]][[2]] - 20, 
     data[[i]][[2]] + 20}}]

(*Here listy is the index of each neighboring point, calculated before*)

Table[Show[pNeighbs2[ii], pPar[ii] ], {ii, 200, 205}]

The result shows like this image,

I want to label each point (both reds and blacks) with its index next to it.

enter image description here

$\endgroup$
3
  • $\begingroup$ hi. Your code does not run. I get lots of errors, missing data, etc... $\endgroup$
    – Nasser
    Apr 3, 2020 at 4:54
  • $\begingroup$ Well I excluded the part of the code where you generate a list because it is very lengthy $\endgroup$
    – Erfi
    Apr 3, 2020 at 5:07
  • 2
    $\begingroup$ to make it easy to answer you, one needs to run your code. How else do you expect someone to answer you otherwise? You could always make a MWE. $\endgroup$
    – Nasser
    Apr 3, 2020 at 5:13

1 Answer 1

3
$\begingroup$
SeedRandom[1]
data = RandomReal[300, {300, 2}];

Construct lists of neighbors within radius 20 of each point using Nearest:

r = 20;
listy = Association @ 
   MapIndexed[#2[[1]] -> Rest@Nearest[data -> "Index", #, {All, r}] &, data];

You can wrap each data point with Callout:

pPar[i_] := ListPlot[{Callout[data[[i]], i]}, AspectRatio -> 1, 
  BaseStyle -> PointSize[Large], PlotStyle -> Black, 
  PlotRange -> ({-20, 20} + # & /@ data[[i]])]

pNeighbs2[i_] := ListPlot[Table[Callout[data[[listy[i][[j]]]], listy[i][[j]]], {j, 1, 
    Length[listy[i]]}], AspectRatio -> Automatic, 
  BaseStyle -> PointSize[Large], PlotStyle -> Red, 
  PlotRange -> ({-20, 20} + # & /@ data[[i]])]

Row[Table[Show[pNeighbs2[ii], pPar[ii], ImageSize -> 1 -> 4], {ii, 200, 204}],
 Spacer[10]]

![enter image description here

Slightly more convenient approach is to plot a point and its neighbors in a single ListPlot to avoid overlapping labels:

ClearAll[color, lP]
color[1] = Black;
color[_] := Red;
lP = ListPlot[#, PlotRange -> (Function[x, {-20, 20} + x] /@ #[[1, 1, 1]]), ##2] &; 

assoc = Association @ MapIndexed[#2[[1]] -> 
  MapIndexed[Callout[Style[data[[#]], Directive[PointSize[Large], color[#2[[1]]]]], #] &, 
      Nearest[data -> "Index", #, {All, r}]] &, 
  data];

Row[lP[#, ImageSize -> 1 -> 4, Frame -> True, AspectRatio -> 1] & /@ 
  assoc /@ Range[200, 204], Spacer[10]]

enter image description here

Grid[Partition[
  lP[#, ImageSize -> 1 -> 4, Frame -> True, AspectRatio -> 1, Axes -> False] & /@ 
    assoc /@ Range[100, 114], 5]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you, I am trying your first approach but my version of mathematica 9 doesn't seem to know Callout , do you know how to call it ? $\endgroup$
    – Erfi
    Apr 3, 2020 at 19:05
  • $\begingroup$ @Erf, Callout is available in versions 10+. For versions before v10.0 replace Callout with Labeled (you lose the connector lines between the points and labels) $\endgroup$
    – kglr
    Apr 3, 2020 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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