2
$\begingroup$

Is there a way to control the size of self loops when rendering a Graph?

Example:

Graph[{1 -> 2, 1 -> 1}, VertexSize -> {1 -> 0.1, 2 -> 0.2},  PerformanceGoal -> "Quality"]

Mathematica graphics

How can I make the loop bigger or smaller?

I do not want to use a custom EdgeShapeFunction. Bult-in EdgeShapeFunctions are acceptable.

When using a user-defined edge shape function, the nice positioning of the arrowheads will be broken in the above graph. When using a built-in one, such as EdgeShapeFunction -> "CarvedArrow", everything is fine. Perhaps some of the builtin ones have an option to control the self loop size? Some do have options, but they are undocumented.

GraphPlot has SelfLoopStyle. Is there anything for Graph?

$\endgroup$

1 Answer 1

3
$\begingroup$

You could define your own scaling arc function:

arcFunc[g_, r_: 1.5][list_, DirectedEdge[x_, x_]] :=

 With[{v = 
    DynamicLocation["VertexID$" <> ToString[VertexIndex[g, x]], 
     Automatic, Center]}, 
  Arrow[BezierCurve[
    Join[{v}, 
     ScalingTransform[r {1, 1}, list[[1]]][
      list[[{5, 8, 10, 16, 18, 21}]]], {v}], SplineDegree -> 7]]]

g = Graph[{1 -> 2, 1 -> 1}, VertexSize -> {1 -> 0.1, 2 -> 0.2}, 
  EdgeShapeFunction -> {x_ \[DirectedEdge] x_ :> arcFunc[g, 1.7]}, 
  PerformanceGoal -> "Quality"]

or

g = Graph[{1 -> 2, 1 -> 1}, VertexSize -> {1 -> 0.1, 2 -> 0.2}];
Graph[g, EdgeShapeFunction -> {x_ \[DirectedEdge] x_ :> 
    arcFunc[g, 1.7]}, PerformanceGoal -> "Quality"]
$\endgroup$
11
  • $\begingroup$ I did not know that it was possible to use a pattern when setting options such as EdgeShapeFunction. I am referring to x_ \[DirectedEdge] x_. Do you know where this is documented? $\endgroup$
    – Szabolcs
    Commented May 15, 2017 at 14:01
  • 2
    $\begingroup$ @Szabolcs It's documented under scope in EdgeShapeFunction documentation page. $\endgroup$
    – halmir
    Commented May 15, 2017 at 16:08
  • $\begingroup$ This answer does not work when I have specified EdgeLabels: Graph[{1 -> 1, 1 -> 2}, EdgeLabels -> {(1 -> 1) -> "E1", (1 -> 2) -> "E2"}, EdgeShapeFunction -> {x_ \[DirectedEdge] x_ :> arcFunc[g, 1.7]}]. How could I resolve this? $\endgroup$
    – Hotschke
    Commented Oct 5, 2018 at 10:43
  • $\begingroup$ @Hotschke this function need to set PerformanceGoal -> "Quality". g = Graph[{1 -> 1, 1 -> 2}, EdgeLabels -> {(1 -> 1) -> "E1", (1 -> 2) -> "E2"}, EdgeShapeFunction -> {x_ [DirectedEdge] x_ :> arcFunc[g, 1.7]}, PerformanceGoal -> "Quality"] $\endgroup$
    – halmir
    Commented Oct 5, 2018 at 14:54
  • $\begingroup$ Thanks for your answer. It works now. However, I do not know why this has something to do with PerformanceGoal. I cannot find an explanation in the documentation under EdgeShapeFunction or PerformanceGoal. Can you give me a hint? $\endgroup$
    – Hotschke
    Commented Oct 5, 2018 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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