5

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.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • I don't have Mathematica 8. Can you try for `ImagePadding` each of: `Automatic`, `Full`, `All` please? – Mr.Wizard May 09 '11 at 14:33
  • The default is `Automatic`. `Full` and `All` give the same result. – Szabolcs May 09 '11 at 14:38
  • 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 May 09 '11 at 14:42
  • I see in the v8 online help that ImagePadding is used for this purpose. I suspect that means there is no easy, automatic way to deal with this generally. It could be a bug or oversight. What is the output type of `Graph`? What do you get if you do `g = Graph[ ... ]` then `g[[1]]`? – Mr.Wizard May 09 '11 at 14:49
  • Can and explicit `Automatic` produce different results even if it appears explicitly in `Options[someSymbol]`? Actually the default here is `All`, I was mistaken. The structure of a `Graph`object is pretty much the same as what we type as input, brought to a canonical (?) form. In this case `Graph[{1, 2, 3}, {DirectedEdge[1, 2], DirectedEdge[2, 3], DirectedEdge[3, 1]}, {VertexLabels -> {"Name"}}]` – Szabolcs May 09 '11 at 14:55
  • I was hoping it was a Graphics object. A long shot here, but what happens if you apply FullGraphics to a Graph object? – Mr.Wizard May 09 '11 at 15:11
  • (So this is the structure that a function such as `Part` would see in `Graph`, however, because of some bugs with `Graph` I believe that Mathematica uses a more low level representation internally.) – Szabolcs May 09 '11 at 15:11
  • @Mr.Wizard, it fixes the problem and removes the interactivity. (One can right click a `Graph` and select layout options etc.) I don't use the interactive features too often though. Visible labels are much more important. – Szabolcs May 09 '11 at 15:13
  • If that does some good I'll post it as an answer for other people to find. – Mr.Wizard May 09 '11 at 15:15
  • What happens if you do `GraphPlot[ Graph[...] ]`? – Mr.Wizard May 09 '11 at 15:16
  • `GraphPlot` supports `Graph` in Mma 8. Plots it as usual (i.e. GraphPlot-style output, not Graph-style interactive object). So now we have 3 different semi-(in?)-compatible ways to handle graphs ... `Graph`, `GraphPlot` & `GraphUtilities` package, and Combinatorica. – Szabolcs May 09 '11 at 15:21
  • I was wrong, `Graph` is actually atomic, `Part` won't work on it, and `AtomQ` returns `True`. See also http://stackoverflow.com/questions/4301833/new-graph-in-mathematica-8-0 – Szabolcs May 09 '11 at 15:56
  • That sounds less than ideal, but what do I know. – Mr.Wizard May 09 '11 at 15:58
  • 2
    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 May 09 '11 at 19:41
  • 2
    I've reported this in December and got confirmation that it's a known bug – Yaroslav Bulatov May 10 '11 at 04:45
  • I'm still having this problem in Mathematica 11 – Matt G Mar 14 '17 at 20:11

2 Answers2

2

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.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 2
    I wouldn't use that as FullGraphics changes many aspects of the output. Try, e.g., `FullGraphics[ Graph[{1 -> 2, 2 -> 3, 3 -> 1}, VertexLabels -> "Name", PlotLabel -> "This is a test", BaseStyle -> {FontFamily -> "Arial-Bold", FontSize -> 16}]]` and replace `FullGraphics` with `Show`. – Sjoerd C. de Vries May 09 '11 at 19:35
  • @Sjoerd, good to know. Other than rushed deadlines, can you think of a reason why `Graph` would clip labels by default? – Mr.Wizard May 09 '11 at 19:47
  • Not really. As I noted in my comment below the question it seems WRI is fully aware of the problem since they use the ImagePadding workaround in the VertexLabel doc section quite lot. – Sjoerd C. de Vries May 09 '11 at 20:57
  • Graph has default setting `ImagePadding->All` which is supposed to mean that all objects are included. I guess that for text objects they may use their anchor point to determine inclusion in the plot, which leads to errors for large fonts and large texts. – Sjoerd C. de Vries May 09 '11 at 21:20
1

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.

JeremyKun
  • 2,987
  • 2
  • 24
  • 44