6

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Kagaratsch
  • 11,955
  • 4
  • 25
  • 72

1 Answers1

7

Update: With new-in-version-12.1 functionEdgeTaggedGraph we can style and label edges individually:

stylededges = Style @@@ Thread[{ a->b, {Red, Blue, Green, Orange}}];

EdgeTaggedGraph[stylededges, EdgeLabels -> "Name", ImageSize -> Large]

enter image description here

Original answer:

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

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