11
$\begingroup$

What's the simplest way to plot a graph with weighted edges, such that the color of the edge corresponds to the weight of the edge?

$\endgroup$
  • 2
    $\begingroup$ Why the downvote? Please leave a comment $\endgroup$ – becko Sep 2 '14 at 19:04
  • 1
    $\begingroup$ The evildoer was me, because I felt that an estimated contributor like yourself should have shown some own efforts. $\endgroup$ – eldo Sep 2 '14 at 19:32
  • $\begingroup$ @eldo I really did not know how to get started, so I had nothing to show. $\endgroup$ – becko Sep 2 '14 at 20:01
  • $\begingroup$ Accepted @ becko $\endgroup$ – eldo Sep 2 '14 at 20:06
15
$\begingroup$

Update: If the graph g1 is already created, I think SetProperty is the most convenient way to make changes in g1:

g1 = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
ew = PropertyValue[g1, EdgeWeight];
el = EdgeList[g1];
edgestylea = Thread[el -> (Directive[CapForm["Round"],
        Thickness[Rescale[# , Through@{Min, Max}@ew, {0.02, .06}]],
        ColorData[1, #]] & /@ ew)];
edgestyleb = Thread[el -> (Directive[CapForm["Round"],
        Thickness[.02 + .04 #], (* or Thickness[Rescale[#, {0,1}, {.02,.06}]] *)
        ColorData["SolarColors"][#]] & /@ Rescale[ew])];

g1a = SetProperty[g1, EdgeStyle -> edgestylea];
g1b = SetProperty[g1, EdgeStyle -> edgestyleb];
Row[{g1a, g1b}]

enter image description here

If not, you can directly use the edge-weight information for styling edges:

el = {1 <-> 2, 2 <-> 3, 3 <-> 1};
ew = {2, 3, 4};
edgestylea = Thread[el -> (Directive[CapForm["Round"],
        Thickness[Rescale[# , Through@{Min, Max}@ew, {0.02, .06}]],
        ColorData[1, #]] & /@ ew)];
edgestyleb = Thread[el -> (Directive[CapForm["Round"],
        Thickness[.02 + .04 #],
        ColorData["SolarColors"][#]] & /@ Rescale[ew])];

g2a = Graph[el, EdgeWeight -> ew, EdgeStyle -> edgestylea];
g2b = Graph[el, EdgeWeight -> ew, EdgeStyle -> edgestyleb];
Row[{g2a, g2b}]
(* same picture *)

Original Post

One possible approach: use the EdgeWeight PropertyValue of an edge with EdgeStyle

g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4},
     EdgeStyle -> {e_ :> 
                Directive[Thick, ColorData[1, PropertyValue[{g, e}, EdgeWeight]]]}]

enter image description here

Or

h = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeWeight -> {2, 3, 4}];
SetProperty[h, EdgeStyle -> Thread[EdgeList[h] ->
            (Directive[Thick, ColorData[1, #]] & /@  PropertyValue[h, EdgeWeight])]]
(* same picture *)
$\endgroup$
  • 2
    $\begingroup$ Nice. I didn't realise you could use patterns in EdgeStyle. FYI you can use <-> for a slightly more readable UndirectedEdge. $\endgroup$ – Simon Woods Sep 2 '14 at 19:16
  • 1
    $\begingroup$ Thank you @Simon. I cannot find this usage pattern in the docs; I must have learned it somewhere on this site. $\endgroup$ – kglr Sep 2 '14 at 19:33
4
$\begingroup$

What's the simplest way...

I think the simplest way is with the IGraph/M package.

ExampleData[{"NetworkGraph", "EastAfricaEmbassyAttacks"}] //  
  Graph[#, EdgeStyle -> AbsoluteThickness[5]] & // (* thicken edges *)
  IGEdgeMap[ColorData["Rainbow"], EdgeStyle -> Rescale@*IGEdgeProp[EdgeWeight]] (* colour edges *)

enter image description here

Notice how setting EdgeStyle individually for each edge (to colour them) did not remove the global EdgeStyle setting that adjusts thickness. I am pointing this out because this behaviour is far from obvious (in fact to me it is counterintuitive), but it can be very useful.

The setting stored inside the graph looks like this:

enter image description here

$\endgroup$
  • $\begingroup$ Disclosure: I'm the package author. While this looks like a plain advertisement, I really do think that this technique considerably simplifies the task of styling graphs based on their attributes. I use it regularly. $\endgroup$ – Szabolcs Nov 30 '17 at 17:46

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.