Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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] ? –  b.gatessucks Oct 22 '12 at 12:19
    
hmm, why then in the documentation example does #1 work? I thought the first argument is the x->y connections? –  Ooku Oct 22 '12 at 12:22
    
I just read the first point under More Information here and it seems that you need the vertices (2nd argument). –  b.gatessucks Oct 22 '12 at 12:24
    
Ah, mis-read "beginning and end points" as "beginning and end vertices". Thanks. –  Ooku Oct 22 '12 at 12:35
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks, although to be precise, direction matters, but otherwise, this is the answer I was looking for –  Ooku Oct 22 '12 at 12:36
    
In that case, replace MemberQ[#2, 1] with First[#2] == 1. –  J. M. Oct 22 '12 at 12:39
    
@J.M. Out of curiosity, how come the image is flipped ? –  b.gatessucks Oct 22 '12 at 14:30
    
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). –  J. M. Oct 22 '12 at 14:33
    
@J.M. Same setup at home, will try later. –  b.gatessucks Oct 22 '12 at 14:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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