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

1 Answer 1

7
$\begingroup$

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

$\endgroup$
2
  • $\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$ Mar 14, 2017 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, 2017 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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