# Graph: Coloring parallel edges individually

CODE:

Graph[{Style[0 -> 1, Red], Style[0 -> 1, {Blue, Dashed}]}]


This Mathematica code will make both lines solid Red, since they belong to the same two vertices and have the same direction.

I want one of them to be red, the other blue dashed for instance. How can I do this?

• Why is this solution not working for you ? (4501) – Sektor May 26 '15 at 20:14
• Both lines end up being red! – johan Carlstrom May 26 '15 at 20:28
• Did you try adapting it to your case, not just copy-pasting it ? – Sektor May 26 '15 at 20:29
• Yes well I wrote a script to generate these things. But I realise that once you set the style for the line from 0->1, then it can not change. I looked into the mathematica examples on their website, but all of them are very simple and give no hint as to how this problem can be solved... – johan Carlstrom May 26 '15 at 20:32

Update 3: With the new-in-version-12.1 function EdgeTaggedGraph we can style and label edges individually in multi-graphs:

labels = {"A", "B", "C", "D", "E", "F"};
edges = {a -> b, a -> b, a -> b, a -> b, a -> e, e -> b};
styles = {Red, Directive[Dashed, Blue], Orange,
Directive[Purple, Dashing[.01]],  Green, Green};

labelededges = MapThread[Style[Labeled[#, #2], #3] &, {edges, labels, styles}] ;

EdgeTaggedGraph[labelededges, EdgeLabels -> "Name",
ImageSize -> Medium, BaseStyle -> Thick, EdgeLabelStyle -> 16,
VertexLabelStyle -> 16, PlotTheme -> "DiagramGold"]


Update 2: A much more convenient approach to construct a custom EdgeShapeFunction to style multi-edges individually:

styles = Association[PropertyValue[g1, EdgeStyle]] ;
esf = {Dashing[{}], First[styles[#2] = RotateRight[styles[#2]]],
Graph[g1, EdgeShapeFunction -> esf]


Update: To make it more convenient to specify precisely the color (style) of each edge in a multigraph, not resort to a fixed sequence of styles as suggested by @David G Stork in the comments:

Specify edge labels for each edge using EdgeStyle:

g1 = Graph[{a -> b, a -> b, a -> b, a -> b, a -> c, a -> c, a -> c, c  -> b},
VertexLabelStyle -> 18, VertexLabels -> Placed["Name", Center],
GraphLayout -> "LayeredDigraphEmbedding", GraphStyle -> "DiagramGold",
EdgeStyle -> {(a -> b) -> {Red, Directive[Dashed, Blue], Orange,
Directive[Purple, Dashing[.01]]},
(a -> c) -> {Green, Cyan, Yellow}, (c -> b) -> {Pink}}];


In g1 multi-edges are colored with a single color.

Extract the styles for desired edge (e) into the variable style[e], and initialize the variable index[e] to 1.

ClearAll[index, style]
distinctedges = DeleteDuplicates[EdgeList[g1]];
(style[#] = PropertyValue[{g1, #}, EdgeStyle])& /@ distinctedges;
(index[#] = 1) & /@  distinctedges;


Inject the multiple styles for each edge using EdgeShapeFunction:

g2 = Fold[(SetProperty[{#,  #2}, EdgeShapeFunction ->
({Arrowheads[Large], Thick, style[#2][[index[#2]++]], Arrow[#, .1]} &)]) &,
g1,  distinctedges];

Row[{g1, g2}]


You can use EdgeShapeFunction:

styles={Red, Directive[Dashed, Blue], Orange, Directive[Purple, Dashing[.01]],
Green, Green};
i = 1;
Graph[{a -> b, a -> b, a -> b, a -> b, a -> e, e -> b},
VertexLabels->"Name"]


If you have at most two edges between a pair of vertices, you can also cheat using the Arrowheads option:

Graph[{Style[0 -> 1, {Arrowheads[.04], Red}], Style[1 -> 0,
{Blue, Arrowheads[{-.04, 0.}], Dashed}], 0 -> 2, 2 -> 1},
VertexLabels -> "Name", ImagePadding -> 10]


• Yes this is exactly what I am looking for. Thank you very much ! – johan Carlstrom May 26 '15 at 20:38
• @johanCarlstrom, my pleasure. Welcome to mma.se. – kglr May 26 '15 at 20:51
• This isn't QUITE what I need. I want to be able to specify precisely the color (style) of each edge in a multigraph, not resort to a fixed sequence of styles (as given by @kglr. How do I do that? – David G. Stork Mar 14 '17 at 21:39
• Can we trust that the EdgeShapeFunction is applied in a consistent order, so that if I give a list of styles in the same order as EdgeList, they will consistently be applied to the corresponding edges? I think you had an answer which solved this but not sure which one ... Motivation: mathematica.stackexchange.com/a/199541/12 – Szabolcs Jun 1 '19 at 19:47
• @Szabolcs, I am not sure in which order EdgeShapeFunction processes multiple edges. – kglr Jun 2 '19 at 5:21