I have the following edgelist and colors:

edges = {0 <-> 1, 0 <-> 2, 0 <-> 1, 2 <-> 1, 0 <-> 1};
colors = {Blue, Red, Red, Yellow, Green};

But I can't find a way to color the edges using these colors. My first try of course was to use something like:

Graph[edges, EdgeStyle -> {0 <-> 1 -> Blue,  0 <-> 2 -> Red, 0 <-> 1 -> Red, 
  2 <-> 1 -> Yellow, 0 <-> 1 -> Green}] 

But in this all of the edges between 0 and 1 turn out to be green even though only one of them should be. I also tried to do this with

colors = {Blue, Red, Red, Yellow, Green};
i = 1;
Graph[{0 <-> 1, 0 <-> 2,  0 <-> 1, 2 <-> 1,  0 <-> 1}, 
EdgeShapeFunction -> ({Arrowheads[Large], Thick, colors[[i++]], 
 Arrow @ #} &)]

But now a wrong edge gets the green color for some reason. Any ideas? I already looked into Graph: Coloring parallel edges individually which didn't help me with this really.

  • 1
    $\begingroup$ Mathematica does not properly support edge properties for multigraphs. It is not possible to assign a separate EdgeStyle property to different edges that run between the same vertices. If you care about this functionality, you should contact Wolfram Support and request it. I believe they will only fix this if many people request it. $\endgroup$ – Szabolcs Oct 19 '18 at 13:26
  • $\begingroup$ @Szabolcs Really? That's a shame for me at least. $\endgroup$ – Kplusn Oct 19 '18 at 14:07
  • $\begingroup$ See here: mathematica.stackexchange.com/q/92014/12 EdgeWeight, EdgeCapacity and EdgeCost are the only properties that work for multigraphs (if you're careful). Unfortunately, this limitation is not properly documented, things just fail ... However, development of Graph has just picked up again (there was a recent Twitch streams about it), so it's the best time to request this feature again. If you believe you need this, please do write to Wolfram Support and request it. $\endgroup$ – Szabolcs Oct 19 '18 at 14:22
  • $\begingroup$ @Szabolcs Sure, will do! But couldn't I go around it then and define different weights or costs to edges and then color them according to the weight/cost? Or will it still fail? $\endgroup$ – Kplusn Oct 19 '18 at 14:29
  • $\begingroup$ The issue is that EdgeStyle cannot be used. Since Mathematica refers to edges by their endpoints, there is no way to distinguish between parallel edges, and assign different properties to them. Colouring is normally done through EdgeStyle. That won't work here. Thus any solution one might come up with will be inconvenient and error-prone. $\endgroup$ – Szabolcs Oct 19 '18 at 14:34
edges = {0 \[UndirectedEdge] 1, 0 \[UndirectedEdge] 2,  
  0 \[UndirectedEdge] 1, 2 \[UndirectedEdge] 1,  0 \[UndirectedEdge] 1};
colors = {Blue, Red, Red, Yellow, Green};
styles = GroupBy[Thread[edges -> colors], First -> Last]

enter image description here

esf = {First[styles[#2] = RotateRight[styles[#2]]], Arrow[#]} &;

Graph[edges, EdgeStyle -> Normal[styles], EdgeShapeFunction -> esf, 
 VertexLabels -> "Name", EdgeLabels -> "Name" ]

enter image description here


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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