5

Bug introduced in 9.0.1 or earlier and persisting through 11.0.1 or later


Recently I found the following TreePlot issue. For simplicity, I took an example from Mathematica Tree Drawing Tutorial and set TreePlot orientation to Left:

TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8, 7 -> 9}, Left,
    VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2], 
        Black, Text[#2, #1]} &)]

TreePlot with Left orientation

But when I specified the root node to 5 (which differs from an automatically selected root node 4), I recieved an image with stretched verticies:

TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8, 7 -> 9}, Left, 5,
    VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2], 
        Black, Text[#2, #1]} &)]

TreePlot with Left orientation and root node 5

Is there a generic solution to this problem? Thanks.

Symbolist
  • 71
  • 3

2 Answers2

6

It seems that there is a bug in how TreePlot handles its default AspectRatio -> Automatic option:

tp = TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8,
    7 -> 9}, Left, 5, 
  VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2],
       Black, Text[#2, #1]} &), AspectRatio -> Automatic]
Options[tp, AspectRatio]

plot

{AspectRatio -> 0.632456}

One workaround is to reset this option to Automatic again using Show:

Show[TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8,
    7 -> 9}, Left, 5, 
  VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2],
       Black, Text[#2, #1]} &)], AspectRatio -> Automatic]

plot

The reason why the form of a disk is affected by AspectRatio is that you specify the disk radius in the scale of intrinsic coordinate system of the Graphics object:

Row[Graphics[Disk[{0, 0}, 1], Frame -> True, ImageSize -> 150, 
    AspectRatio -> #] & /@ {1, .5}]

plots

If you specify the disk radii via Offset (in printer's points) the form of the disk will never be affected by AspectRatio:

TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8, 
  7 -> 9}, Left, 5, 
 VertexRenderingFunction -> ({EdgeForm[Black], Yellow, 
     Disk[#1, Offset[{20, 20}]], Black, Text[#2, #1]} &)]

plot

As suggested in the comments, it is easier to adjust the disk radius if one specify it in the form Offset[r {1,1}] where r is the radius in printer's points.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
0
TreePlot[{1 -> 4, 1 -> 5, 2 -> 8, 3 -> 4, 4 -> 8, 6 -> 8, 7 -> 8, 7 -> 9}, 
     Left, 5, VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2], 
     Black, Text[#2, #1]} &), AspectRatio -> .5]

enter image description here

ciao
  • 25,774
  • 2
  • 58
  • 139