7

How Can I represent with thickness of the edges the weights of this graph?

Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {1, 3, 5}]

Thanks in advance

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574

3 Answers3

7

EdgeStyle can plot the edges with different thicknesses

spec = {1 <-> 2, 2 <-> 3, 3 <-> 1};
thickness = {0.01, 0.02, 0.03};
Graph[spec, EdgeStyle -> Thread[spec -> (Thickness[#] & /@ thickness)]]

enter image description here

Here the thickness of the lines is 1/10 of the original edgeweights, otherwise they are very thick. You can also specify the color the same way:

spec = {1 <-> 2, 2 <-> 3, 3 <-> 1};
thickness = {0.01, 0.02, 0.03};
color = {0., 0.5, 0.6};
Graph[spec, EdgeStyle -> Thread[spec -> 
      Thread@{(Hue[#] & /@ color), (Thickness[#] & /@ thickness)}]]

enter image description here

bill s
  • 68,936
  • 4
  • 101
  • 191
6

With the IGraph/M package, it is very easy to do styling like this. You will find several similar examples in the documentation under Utilities → Property Handling Functions.

IGEdgeMap[
 AbsoluteThickness[1.5 #] &,
 EdgeStyle -> IGEdgeProp[EdgeWeight],
 g
]

What does IGEdgeMap do? It Maps the function AbsoluteThickness[1.5 #] & over the list returned by IGEdgeProp[EdgeWeight][g], and assigned the result to the EdgeStyle edge property of g.

IGEdgeProp[EdgeWeight] is a property extractor. It is a function that, when applied to the graph g, will return the list of EdgeWeights. You can extract any other edge property this way.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
4

Since you can't refer to a PropertyValue of the graph you are currently constructing, you could have an auxiliary graph that holds the properties, and another graph that draws based on those properties.

For example you could make the color and thickness depend on the weight via

g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 8, 14}];

shapeFunc = With[{weight = PropertyValue[{g, #2}, EdgeWeight]},
    {AbsoluteThickness[weight], Hue[weight/14.], Line@#1}] &;
Graph[g, EdgeStyle -> Blue, EdgeShapeFunction -> shapeFunc]

The arguments given to the EdgeShapeFunction are 1) the vertex coordinates for the endpoints of the line segment, and 2) the edge itself.

Mathematica graphics

Jason B.
  • 68,381
  • 3
  • 139
  • 286