4
$\begingroup$
d = {{0, 4, 6, 7.5, 9, 20, 10, 16, 8}, {4, 0, 6.5, 4, 10, 5, 7.5, 11, 
    10}, {6, 6.5, 0, 7.5, 10, 10, 7.5, 7.5, 7.5}, {7.5, 4, 7.5, 0, 10,
     5, 9, 9, 15}, {9, 10, 10, 10, 0, 10, 7.5, 7.5, 10}, {20, 5, 10, 
    5, 10, 0, 7, 9, 7.5}, {10, 7.5, 7.5, 9, 7.5, 7, 0, 7, 10}, {16, 
    11, 7.5, 9, 7.5, 9, 7, 0, 10}, {8, 10, 7.5, 15, 10, 7.5, 10, 10, 
    0}};
WeightedAdjacencyGraph[Range[0, 8], d, 
 EdgeLabels -> Placed[EdgeWeight, Tooltip], VertexLabels -> "Name"]

How can I label the distance information on each edge of the graph?How can I label the distance information on each edge of the graph? It's better to use various colors to label.

WeightedAdjacencyGraph[Range[0, 8], d, 
 EdgeLabels -> 
  Flatten[Table[
    UndirectedEdge[i, j] -> Style[d[[i, j]], Red], {i, 1, 8}, {j, i, 
     8}]], VertexLabels -> "Name"]

But it's not correct.

$\endgroup$
3
$\begingroup$

Change the option setting for EdgeLabels to

EdgeLabels -> Placed["EdgeWeight", Center]

to get

enter image description here

wag = WeightedAdjacencyGraph[Range[0, 8], d, VertexLabels -> "Name"];
styles = Association @ MapIndexed[# -> ColorData[97][#2[[1]]] &, EdgeList@wag];
styledlabels = Association @  MapIndexed[# -> 
   Style[PropertyValue[{wag, #}, EdgeWeight],  ColorData[97][#2[[1]]]] &, 
  EdgeList@wag];
legend = Grid[Partition[Row[#, Spacer[5]] & /@ 
     Transpose[{KeyValueMap[Style[#, #2] &]@styles, Values @ styledlabels}], 5], 
    Alignment -> {Left, Center}, Spacings -> {3, .5}];

Row[{SetProperty[wag,
   {ImageSize -> Medium, VertexSize -> Medium, 
    VertexStyle -> White, VertexLabels -> Placed["Name", Center], 
    EdgeStyle -> Normal @ styles, 
    EdgeLabels -> {e_ :> Placed[styledlabels@e, Center]}}], legend}, 
 Spacer[10]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ And how to display this information in multiple colors? $\endgroup$
    – user69323
    Feb 2 '20 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy