5
votes

Graph[] has a tendency to cut off vertex labels in Mathematica. I am looking for a robust workaround.

Example:

Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels -> "Name"]

Graph[] cuts off the label '2'

My present workaround:

SetOptions[Graph, ImagePadding -> 12]

This is not robust because the value of ImagePadding needs to be manually adjusted depending on the label size.

2
I don't have Mathematica 8. Can you try for ImagePadding each of: Automatic, Full, All please? - Mr.Wizard
The default is Automatic. Full and All give the same result. - Szabolcs
Okay. I figured it was worth a shot. Also, don't assume that Automatic will always produce the same result as no explicit setting; sometimes it does not! - Mr.Wizard
The docs for the Graph option VertexLabels use ImagePadding quite a lot. To me that suggests WRI is aware of the problem and this is the official workaround. - Sjoerd C. de Vries
I've reported this in December and got confirmation that it's a known bug - Yaroslav Bulatov

2 Answers

2
votes

Apparently using FullGraphics on the Graph object will fix the clipping for the purpose of display, at the expense of interactivity.

Per the comment below, Show[] works as well, and avoids modifying the graphics.

1
votes

Here are two possible workarounds.

Enlarge the vertex size and place the labels within the vertex. Of course, this also depends on the length of the labels, but for shortish labels it works well, whereas your example above clips off any label of more than one character for vertex 1.

ex:

Table[Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexSize -> 0.3, 
    VertexLabels -> Table[i -> 
       Placed["vertex" <> ToString[i], p], {i, 3}],
    VertexShapeFunction -> "Square", PlotLabel -> p], 
 {p, {Left, Top, Right, Bottom, Center}}]

Use tooltips to store the labels instead of displaying them on the graphic. [Edit: Center probably looks the best, and then you can wrap labels by putting \n in your string if you need to, but again, depends on the label length.]

ex:

Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels -> Placed["Name", Tooltip]]

While this stops you from being able to see all the labels at the same time, you never have any clipping.