# how to add labels of points next to it based on index in ListPlot

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.

• hi. Your code does not run. I get lots of errors, missing data, etc... Commented Apr 3, 2020 at 4:54
• Well I excluded the part of the code where you generate a list because it is very lengthy
– Erfi
Commented Apr 3, 2020 at 5:07
• 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. Commented Apr 3, 2020 at 5:13

## 1 Answer

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


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


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


• 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 ?
– Erfi
Commented Apr 3, 2020 at 19:05
• @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)
– kglr
Commented Apr 3, 2020 at 19:15