1
$\begingroup$

I have a graph, and I want my EdgeRenderingFunction to colour the edges based on the vertex they originate from. In this toy example, I'm trying to get the edge originating from vertex 1 to be red:

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4, 
  1 -> 4, 3 -> 5, 3 -> 3}, 
 EdgeRenderingFunction -> (If[First[#1] === 1, {Red, Line[#1]}, 
     Line[#1]] &), VertexLabeling -> True]

bad

What am I doing wrong? I was following the (slightly more complicated) model given in the Mathematica documentation:

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4, 
  1 -> 4, 3 -> 5, 3 -> 3}, 
 EdgeRenderingFunction -> (If[
     Length[#1] > 2, {Red, Line[#1], 
      Text[If[First[#1] === Last[#1], "loop", "multiedge"], 
       LineScaledCoordinate[#1, .7], Background -> White]}, 
     Line[#1]] &), VertexLabeling -> True]

good

$\endgroup$
  • $\begingroup$ Maybe GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4, 1 -> 4, 3 -> 5, 3 -> 3}, EdgeRenderingFunction -> (If[First[#2] === 1, {Red, Line[#1]}, Line[#1]] &), VertexLabeling -> True] ? $\endgroup$ – b.gates.you.know.what Oct 22 '12 at 12:19
  • $\begingroup$ hmm, why then in the documentation example does #1 work? I thought the first argument is the x->y connections? $\endgroup$ – Ooku Oct 22 '12 at 12:22
  • $\begingroup$ I just read the first point under More Information here and it seems that you need the vertices (2nd argument). $\endgroup$ – b.gates.you.know.what Oct 22 '12 at 12:24
  • $\begingroup$ Ah, mis-read "beginning and end points" as "beginning and end vertices". Thanks. $\endgroup$ – Ooku Oct 22 '12 at 12:35
4
$\begingroup$

Try this:

GraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4, 1 -> 4, 3 -> 5, 3 -> 3},
          EdgeRenderingFunction -> (If[MemberQ[#2, 1], {Red, Line[#1]}, Line[#1]] &),
          VertexLabeling -> True]

graph with red edges

The docs for EdgeRenderingFunction are a bit subtle, but there is the note that any such function must take the vertices as the second argument.

$\endgroup$
  • $\begingroup$ Thanks, although to be precise, direction matters, but otherwise, this is the answer I was looking for $\endgroup$ – Ooku Oct 22 '12 at 12:36
  • $\begingroup$ In that case, replace MemberQ[#2, 1] with First[#2] == 1. $\endgroup$ – J. M. will be back soon Oct 22 '12 at 12:39
  • $\begingroup$ @J.M. Out of curiosity, how come the image is flipped ? $\endgroup$ – b.gates.you.know.what Oct 22 '12 at 14:30
  • $\begingroup$ I don't quite know why, @b.gatessucks. That's how it came out on the machine I'm using (8.0.4 on Fedora 17). $\endgroup$ – J. M. will be back soon Oct 22 '12 at 14:33
  • $\begingroup$ @J.M. Same setup at home, will try later. $\endgroup$ – b.gates.you.know.what Oct 22 '12 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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