user1394864
user1394864

Reputation:

Coloring edges on graph in Wolfram Mathematica

Is it possible to color an edge of graph in Wolfram Mathematica using color function, that depends on coordinate on the edge? Like using ColorFunction option in Plot[].

I have a function specified on the edge of the graph, depending on coordinate at the edge. Is it possible to paint a dencity of this function on the edge? Thanks for responce.

PS: The first idea - to use Inset[] to plug in graphical colored object in EdgeRenderingFunction, but it seems to be quite unnatural. Is there any simpliar ways?

Upvotes: 4

Views: 825

Answers (1)

kglr
kglr

Reputation: 1438

One way to use ColorFunction to color edges in a Graph is:

  ClearAll[colorededge];
  colorededge[pts_, colorfunc_: Function[{x, y}, ColorData["TemperatureMap"][y]]] := 
  ListPlot[pts, Joined -> True, PlotStyle -> Thick, Axes -> False, 
  ColorFunction -> colorfunc, ColorFunctionScaling -> True];
  edgshpfnc = (If[Last[#2] == "B", First@colorededge[#1], 
  First@colorededge[#1, Function[{x, y}, Blend[{Yellow, Red}, x]]]] &);
  Graph[{"A" -> "B", "B" -> "C", "C" -> "A"}, 
  VertexCoordinates -> {"A" -> {0, 0}, "B" -> {1, 1}, "C" -> {2, 0}}, 
  EdgeShapeFunction -> edgshpfnc, VertexLabels -> "Name",  ImagePadding -> 10]

gives

enter image description here

and

 GraphPlot[{"A" -> "B", "B" -> "C", "C" -> "A"},
 EdgeRenderingFunction -> edgshpfnc, VertexLabeling -> True]

gives

enter image description here

Upvotes: 5

Related Questions