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.

In order to color Edges in a Graph after applying a function such as VertexDegree I did the following code (on Mathematica 8.0.4):

g = GridGraph[{4, 4},VertexLabels->"Name",ImagePadding->10,DirectedEdges->True];
(* highlight is taken from the documentation of VertexDegree *)
highlight = Table[
  Style[VertexList[g][[i]],
    ColorData["TemperatureMap"][VertexDegree[g][[i]]/Max[VertexDegree[g]]]],
  {i, VertexCount[g]}];
edgeList = EdgeList@g;
Flatten@Table[{edgeList[[i]] -> (
  Function[Evaluate@
    Line[#, VertexColors -> {highlight[[edgeList[[i, 1]], 2]], 
       highlight[[edgeList[[i, 2]], 2]]}]])},
  {i,Length@edgeList}];

HighlightGraph[SetProperty[g, EdgeShapeFunction -> %], highlight]

giving me:

Graph with colored edges

which is basically what I was waiting for in terms of what it should look like.

Although since I'm using Line[#, VertexColors -> {Color1, Color2}] the fact that the Edges are either Directed or Undirected is not preserved in the Graph.

Thus my question is:

Is there a way to preserve the DirectedEdge/UndirectedEdge property and to gradually color the Edges in terms of its two vertices?

share|improve this question
    
Quick fix would be to modify your EdgeRenderingFunction with: Function[Evaluate@{Arrow[#], Line[#,___]}]. Do you need something like that but with DirectedEdge/UndirectedEdge? –  Kuba Oct 18 '13 at 18:37
    
Something which takes into account the property of the edge would be great :) –  Öskå Oct 18 '13 at 18:49
    
And I was also hoping that someone could have a solution using EdgeStyle. –  Öskå Oct 18 '13 at 19:02
add comment

1 Answer

up vote 3 down vote accepted

Since VertexColors is only way to set colors of line gradually, I don't think there's way to control with EdgeStyle. One thing you can do is by define two edge shape functions which defer by edges:

eStyle[colors_][pts_, UndirectedEdge[x_, y_]] := 
 (Line[pts, VertexColors -> {colors[[x]], colors[[y]]}]) 
eStyle[colors_][pts_, DirectedEdge[x_, y_]] :=
  {colors[[y]], Arrow[Line[pts, VertexColors -> {colors[[x]], colors[[y]]}]]}

Directed graph:

g = GridGraph[{4, 4}, VertexLabels -> "Name", ImagePadding -> 10,  DirectedEdges -> True];

colors = ColorData["TemperatureMap"] /@ (VertexDegree[g]/Max[VertexDegree[g]]);
HighlightGraph[SetProperty[g, EdgeShapeFunction -> eStyle[colors]],
     Thread[Style[VertexList[g], colors]]]

enter image description here

Undirected graph:

g = GridGraph[{4, 4}, VertexLabels -> "Name", ImagePadding -> 10];

colors = ColorData["TemperatureMap"] /@ (VertexDegree[g]/Max[VertexDegree[g]]);
HighlightGraph[SetProperty[g, EdgeShapeFunction -> eStyle[colors]], 
  Thread[Style[VertexList[g], colors]]]

enter image description here

share|improve this answer
add 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.