1
$\begingroup$

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], 
     Arrowheads[0.06]], 
    Boole[MemberQ[N /@ D1, First@coord]] == 0}, {Style[Arrow[coord], 
     Blue, Thickness[.01], Arrowheads[0.06]], 
    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, 
  PlotRangePadding -> .2, 
  VertexLabels -> {Table[{i[[1]], i[[2]]} -> {P[[i]][[1]], 
       P[[i]][[2]]}, {i, Length[P]}]}]
(* [Above] Illustrates Pathway*)

However, we instead get:

enter image description here

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

$\endgroup$

3 Answers 3

3
$\begingroup$

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, 
  PlotRangePadding -> .2, VertexLabels -> Thread[Range[Length[P]] -> ToString /@ P]

![enter image description here

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

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

PathGraph[P, VertexCoordinates -> P, EdgeShapeFunction -> arrow, 
 ImageSize -> 300, PlotRangePadding -> .2, VertexLabels -> Automatic]
$\endgroup$
2
  • $\begingroup$ The problem is it's hard for people to visualize where the points are. $\endgroup$ Commented Aug 16, 2023 at 16:08
  • $\begingroup$ Perhaps you could add the vertex numbers and coordinates. $\endgroup$ Commented Aug 16, 2023 at 16:09
1
$\begingroup$

I found the solution using Table:

Edge = PathGraph[Range[Length[P]], VertexCoordinates -> P, 
  EdgeShapeFunction -> arrow, ImageSize -> 300, 
  PlotRangePadding -> .2, 
  VertexLabels -> Table[i -> String1[[i]], {i, Length[P]}]]

enter image description here

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.