4
$\begingroup$

When I try to color edges in a graph that point to different vertices, it works properly:

Graph[{Style[DirectedEdge[1, 2], Red], Style[DirectedEdge[2, 3], Blue]}]

enter image description here

However, if I try to give different colors to two edges that involve the same vertices, it does not seem to work properly:

Graph[{Style[DirectedEdge[1, 2], Red], Style[DirectedEdge[1, 2], Blue]}]

enter image description here

We see that only one of the colors is registered. How should I apply the coloring properly in this case?

$\endgroup$
  • $\begingroup$ Related Q/A: Label multiple edges between same vertices $\endgroup$ – kglr Dec 2 '16 at 20:27
  • $\begingroup$ Do you mean: Graph[{Style[DirectedEdge[1, 2], Red], Style[DirectedEdge[2, 1], Blue]}]? $\endgroup$ – bill s Dec 2 '16 at 20:28
  • $\begingroup$ @bills No, both edges intentiionally should have the same direction. $\endgroup$ – Kagaratsch Dec 2 '16 at 20:30
  • $\begingroup$ See also comments here: mathematica.stackexchange.com/q/92014/12 There's a fundamental problem with multigraphs and properties ... $\endgroup$ – Szabolcs Dec 2 '16 at 20:39
  • 3
    $\begingroup$ @Kagaratsch, or wait till version 12:) $\endgroup$ – kglr Dec 2 '16 at 20:41
5
$\begingroup$

Modifying the answer in this Q/A:

styles={Red, Blue, Green, Orange};
i=1;
Graph[{a->b,a->b,a->b, a->b}, EdgeShapeFunction->({styles[[i++]],Arrow@#}&)]

Mathematica graphics

Update: dealing with more general cases

Needs["GraphUtilities`"]

styles1 = {Red, Blue, Green, Orange};
styles2 = { Purple, Cyan, Pink};
labels1 = {"A", "B", "C", "D"};
labels2 = { "E", "F", "G"};

Module[{i=1,j=1,i2,j2}, Graph[{a->b,a->b,a->b, a->b, a->c,c->a,b->c,b->c,b->c, b->a}, 
 EdgeShapeFunction->{(a->b)->({styles1[[i2 = i++]], Text[labels1[[i2]],
    LineScaledCoordinate[#, 0.5]], Arrow@#}&),
  (b->c)->({styles2[[j2 = j++]], Text[labels2[[j2]],
    LineScaledCoordinate[#, 0.5]], Arrow@#}&)}, VertexLabels->"Name"]]

Mathematica graphics

Module[{i = 1, j = 1, i2, j2}, 
 Graph[Join[(Property[#, EdgeShapeFunction -> ({styles1[[i2 = i++]], 
          Text[labels1[[i2]], LineScaledCoordinate[#, 0.5]], 
          Arrow@#} &)] & /@ {a -> b, a -> b, a -> b}), 
   (Property[#, EdgeShapeFunction -> ({styles2[[j2 = j++]], 
          Text[labels2[[j2]], LineScaledCoordinate[#, 0.5]], 
          Arrow@#} &)] & /@ {e -> b, e -> b, e -> b}), {a -> e, e -> c, c -> e, a -> c}], 
  VertexLabels -> "Name", 
  EdgeStyle -> {(a -> c) -> Dashed, (a -> b) -> DotDashed , 
         (e -> c) -> Directive[Dotted, Green]}, 
  ImageSize -> 500] ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ This doesn't quite answer the general question of how to specify colors that are not in an assigned order throughout a general multigraph. $\endgroup$ – David G. Stork Mar 14 '17 at 21:50
  • $\begingroup$ @David, i updated with two examples of how this hack could be used in a more complicated setting. $\endgroup$ – kglr Mar 15 '17 at 6:30

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.