13

CODE:

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

enter image description here

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?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263

1 Answers1

13

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

enter image description here

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]]], 
   Arrowheads[Large], Arrow[#, .1]} &;
Graph[g1, EdgeShapeFunction -> esf]

enter image description here

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

enter image description here

Original answer:

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},  
EdgeShapeFunction -> ({Arrowheads[Large],Thick,styles[[i++]],Arrow@#} &),
VertexLabels->"Name"] 

enter image description here

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]

Mathematica graphics

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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: https://mathematica.stackexchange.com/a/199541/12 – Szabolcs Jun 01 '19 at 19:47
  • @Szabolcs, I am not sure in which order EdgeShapeFunction processes multiple edges. – kglr Jun 02 '19 at 05:21
  • Do you have access to the desktop version of M12.1? If yes, it would be really useful for me if you could give IGraph/M a bit of a testing with the new edge tagged graphs. – Szabolcs Mar 19 '20 at 10:10
  • @Szabolcs, I am still using v11.3 on desktop. It will be a few months before I can get desktop v12.1. – kglr Mar 19 '20 at 11:20