4
$\begingroup$

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?

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

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

$\endgroup$

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.