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?
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@#}&)]
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] ]
Comments
Post a Comment