How to label graphs with their weights

A warning! I posted a question when Mathematica v 8.0 was the coolest kid. The error has been resolved since version 9.0.1

The help for EdgeLabels :

enter image description here

But:

 CompleteGraph[4, EdgeWeight -> Range@6, VertexShapeFunction -> "Name", EdgeLabels -> "EdgeWeight"] 

Results in:

enter image description here

So, no border labels ... I think this is a mistake.

I used a nasty construct like:

 adj = {{\[Infinity], 1, 1, 1, 1}, {1, \[Infinity], 2, 2, 2}, {1, 2, \[Infinity], 2, 2}, {1, 2, 2, \[Infinity], 2}, {1, 2, 2, 2, \[Infinity]}}; WeightedAdjacencyGraph[adj, VertexShapeFunction -> "Name", EdgeLabels -> MapThread[Rule,{EdgeList@#,AbsoluteOptions[#, EdgeWeight]/.{_ -> x_}-> x}], GraphHighlight -> FindEdgeCover[#]] &@ WeightedAdjacencyGraph[adj] 

enter image description here

Best ideas?

+7
wolfram-mathematica mathematica-8
source share
4 answers

For regular GraphPlot you will need a slightly more complex solution using the EdgeRenderingFunction ( documentation ). Suppose you have an adjacency matrix, where the elements are also (directional) weights.

 lilnums = {{0, 2., 1., 3., 0, 6.}, {0, 0, 1., 2., 0, 0}, {1., 8., 0, 2., 0, 2.}, {10., 13., 7., 0, 0, 10.}, {0, 0, 0, 0, 0, 0}, {4., 1., 1., 2., 2., 0}} 

Here are a few labels for peaks if you draw network diagrams for international interbank exposures (the original has a lot more countries!).

 names = {"AT", "AU", "CA", "CH", "CL", "ES"} 

The following does what you need. The tricks are a reference to the adjacency matrix, using parts #2 inside the part specification to refer to the correct nums and Mean[#1] elements to find the label in the middle of the edge. Slot #1 seems to contain vertex coordinates.

 GraphPlot[lilnums, DirectedEdges -> True, VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .04], Black, Text[names[[#2]], #1]} &), EdgeRenderingFunction -> ({AbsoluteThickness[2], Red, Arrowheads[0.02], Arrow[#1, 0.05], Black, Text[Round@ Abs[(lilnums[[#2[[1]], #2[[2]]]] + lilnums[[#2[[2]], #2[[1]]]])], Mean[#1], Background -> Yellow]} &), VertexLabeling -> True, ImageSize -> 600, PlotLabel -> Style["Plot Label", Bold, 14, FontFamily -> "Arial"]] 

enter image description here

+9
source share

EdgeLabels β†’ "EdgeWeight" still does not work in 8.0.4 and no longer looks like documentation. However, here is one solution that really works:

 lilnums = {{0, 2., 1., 3., 0, 6.}, {0, 0, 1., 2., 0, 0}, {1., 8., 0, 2., 0, 2.}, {10., 13., 7., 0, 0, 10.}, {0, 0, 0, 0, 0, 0}, {4., 1., 1., 2., 2., 0}} names = {"AT", "AU", "CA", "CH", "CL", "ES"}; g = WeightedAdjacencyGraph[names, lilnums /. {0 -> \[Infinity]}, VertexShapeFunction -> "Name" , ImagePadding -> 15]; SetProperty[g, EdgeLabels -> MapThread[#1 -> #2 &, {EdgeList[g], PropertyValue[g, EdgeWeight]}]] 
+5
source share

EdgeLabels working fine. EdgeWeights does not work.

From the second Belisarius example, it may already be obvious that the problem is EdgeWeights not EdgeLabels

Here are some additional evidence. EdgeLabels displays a lot of labels very well. But when you ask mma to display "EdgeWeights" , it displays 1 incorrectly, regardless of what you saved there.

 CompleteGraph[4, VertexShapeFunction -> "Name", EdgeLabels -> { UndirectedEdge[1, 2] -> "hello", UndirectedEdge[1, 4] -> "goodbye", UndirectedEdge[2, 3] -> 55, UndirectedEdge[3, 4] -> \[Pi]/2, UndirectedEdge[4, 2] -> "\!\(\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(26\)]\)(-1\!\(\ \*SuperscriptBox[\()\), \(i\)]\)\!\(\*SuperscriptBox[\(\[Theta]\), \ \(n - i\)]\)", UndirectedEdge[1, 3] -> {a, b, c}}] 

EdgeWeights.png

The error is not unique to CompleteGraph . Graph and GridGraph have the same problem.

+4
source share

The solution is easy. Switch to V 8.0.1 :)

At least that's what I have, and it works there. (windows 7)

enter image description here

Btw, I don’t know if the marks on the edges are correct, but at least put them on the shape, unlike your image).

+3
source share

All Articles