5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Is this a duplicate? Also at least related: (6440) $\endgroup$ – Mr.Wizard Sep 21 '17 at 22:48
3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ so with your example color and thickness reflect in same time weights? $\endgroup$ – Garuda Ikëtum Sep 21 '17 at 21:11
  • $\begingroup$ and with a network of 500 edges for example how can I have a good thick and good representation? $\endgroup$ – Garuda Ikëtum Sep 21 '17 at 21:14
6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Ok @bill s maybe can it be normalize thickness wiith max value of edgeweights? $\endgroup$ – Garuda Ikëtum Sep 21 '17 at 20:53
  • $\begingroup$ Sure, you can set the values in any desired manner. $\endgroup$ – bill s Sep 22 '17 at 3:00
4
$\begingroup$

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.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.