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
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityHow 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
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.
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)]]
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)}]]
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 Map
s 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 EdgeWeight
s. You can extract any other edge property this way.