# Labeling point coordinates in path graph using VertexLabels

Question: How do we label point coordinates in Pathgraph using VertexLabels.

Suppose we have a list of points S and rearrange them as pathway P.

Clear["Global*"]
S = {{-1.5, 1}, {0, 1}, {1.5, 1}, {1.5, 0.1}, {0, -0.1}, {-1.5,
0}, {1, -1.4}, {-1, -1.5}}; (* Random list of points *)
X[1] = S[[2]];
Y[1] = DeleteCases[S, X[1]];
X[x_] := Nearest[Y[x - 1], X[x - 1], 1][[
1]]; (* Takes X2=x1, X3=x2, X4=x3,... *)
Y[x_] := DeleteCases[Y[x - 1],
X[x]]; (* Deletes X1=x0 from the sample S *)
P = Table[X[x], {x, 1, Length[S]}] (* Lists all points in pathway *)
r[i_] := Flatten[
Position[Nearest[S, P[[i]], Length[S]],
P[[i + 1]]]];(* Lists value r_i  where P[[i+1]] is the r_i th
smallest value from P[[i]] *)
ListofAllr =
Flatten[Table[
r[i]/Length[P], {i, 1,
Length[S] - 1}]](* List of r[i] for all i *)
DeleteAnomalies[ListofAllr]
IndexofNonOutliers =
Flatten[Position[ListofAllr,
Alternatives @@
DeleteAnomalies[
ListofAllr]]](* Index of non-outliers in the list of r *)
D1 = Table[P[[x]],
Evaluate[{x,
IndexofNonOutliers}]](* Excludes integer i where r_i are outliers *)
DistanceD1 =
Table[EuclideanDistance[P[[x]], P[[x + 1]]],
Evaluate[{x,
IndexofNonOutliers}]] ;(* {x,IndexofNon-Outliers} removes the
distance line segments with index i such that r_i is an outlier *)
arrow[coord_, e_] :=
Piecewise[{{Style[Arrow[coord], Red, Thickness[.01],
Boole[MemberQ[N /@ D1, First@coord]] == 0}, {Style[Arrow[coord],
Boole[MemberQ[N /@ D1, First@coord]] ==
1}}];(*Labels arrows for "pathway" P*)


Where we attempt to label the point coordinates of the Pathway using:

Edge = PathGraph[Range[Length[P]], VertexCoordinates -> P,
EdgeShapeFunction -> arrow, ImageSize -> 300,
VertexLabels -> {Table[{i[[1]], i[[2]]} -> {P[[i]][[1]],
P[[i]][[2]]}, {i, Length[P]}]}]
(* [Above] Illustrates Pathway*)


Reformulated Question: How do we fix the code to label the coordinates in the illustration above?

VertexLabels -> {Table[{i[[1]], i[[2]]} -> {P[[i]][[1]], P[[i]][[2]]}, {i, Length[P]}]} is completely wrong syntax. You can not index a running index "i".

To label the vertices by their coordinates, you must change the numerical coordinates into strings like:

Edge = PathGraph[Range[Length[P]], VertexCoordinates -> P,
EdgeShapeFunction -> arrow, ImageSize -> 300,


• Sorry for the sudden upvote. All the coordinates say $(1.5,1)$. How do we fix this? Commented Aug 16, 2023 at 15:45
• Sorry, I did not notice this. It is fixed. Commented Aug 16, 2023 at 18:56

You could just define PathGraph with P and set VertexLabels -> Automatic.

PathGraph[P, VertexCoordinates -> P, EdgeShapeFunction -> arrow,
ImageSize -> 300, PlotRangePadding -> .2, VertexLabels -> Automatic]

• The problem is it's hard for people to visualize where the points are. Commented Aug 16, 2023 at 16:08
• Perhaps you could add the vertex numbers and coordinates. Commented Aug 16, 2023 at 16:09

I found the solution using Table:

Edge = PathGraph[Range[Length[P]], VertexCoordinates -> P,
EdgeShapeFunction -> arrow, ImageSize -> 300,
`