# Differently colored edges of same direction in a graph?

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]}]


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]}]


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

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

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]


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@#}&)]


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"]]


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] ]
`

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