4
$\begingroup$

If I plot the following graph with an EdgeRenderingFunction, the position of both the labels are at dead center, and with a large graph I have the labels completely overlapping each other:

GraphPlot[{{1 -> 2, "a"}, {2 -> 1, "b"}}, 
  DirectedEdges -> True, VertexCoordinateRules -> Automatic, VertexLabeling -> True, 
  EdgeRenderingFunction -> ({Blue, Arrow[#1, 0.1],
     If[#3 =!= None, Text[Style[#3, Medium], Mean[#1], Background -> White], {}]} &)]

Mathematica graphics

Is there a way to position the labels, say, at 60% down the edge instead of dead center, so that the labels don't have a chance to overlap? I've been toggling with Mean[#1] above but can't get this right positioning.

Edit: improved formatting

$\endgroup$
  • $\begingroup$ Edit: The following is a hackish way to do it: If[Length[#1] > 2, Mean[#1] + (#1[[1]] - #1[[2]])*0.5, Mean[#1]], but I would be open to more suggestions. $\endgroup$ – user10245 Oct 28 '13 at 5:36
  • $\begingroup$ Also hackish: Mean[#1[[4 ;;]]] $\endgroup$ – Simon Woods Oct 28 '13 at 14:09
3
$\begingroup$
Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1}, 
 VertexLabels -> "Name", 
 EdgeLabels -> {1 \[DirectedEdge] 2 -> Placed["a", {1/2, {1, 1.5}}], 
   2 \[DirectedEdge] 1 -> Placed["b", {1/2, {1, 1.5}}]}]

enter image description here

$\endgroup$
  • $\begingroup$ Is this the new font default for v10 or did you set fontfamily beforehand? I would welcome a default sans serif label style! $\endgroup$ – István Zachar Nov 27 '13 at 15:17
1
$\begingroup$

Is there a way to position the labels, say, at 60% down the edge instead of dead center

You can use the function LineScaledCoordinate from the GraphUtilities package to set the position of the label along the curve:

Needs["GraphUtilities`"]

GraphPlot[{{1 -> 2, "a"}, {2 -> 1, "b"}}, DirectedEdges -> True, 
 VertexCoordinateRules -> Automatic, VertexLabeling -> True, 
 EdgeRenderingFunction -> ({Blue, Arrow[#1, 0.1], 
   If[#3 =!= None, Text[Style[#3, Medium], LineScaledCoordinate[#, .6], 
       Background -> White], {}]} &)]

enter image description here

Another example:

GraphPlot[{{1 -> 2, "a"}, {1 -> 2, "b"} , {2 -> 1, "c"}, {2 -> 1, "d"}}, 
  DirectedEdges -> True, VertexCoordinateRules -> Automatic, 
  VertexLabeling -> True, 
  EdgeRenderingFunction -> ({Blue, Arrow[#1, 0.1], If[#3 =!= None, 
   Text[Style[#3, Medium, Background -> White], 
       Switch[#3, "a" | "c", LineScaledCoordinate[#, .6], _,  
        LineScaledCoordinate[#, .3]]], {}]} &)]

enter image description here

Update: An alternative that does not require the ``GraphUtilities``` package is to replace LineScaledCoordinate[#, s] above with BezierFunction[#][s].

$\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.