# Labeling vertices by number in Graph: Getting text to display at the right size

Minor aesthetic question here. I was playing around with Graph's and decided to render a cute-looking family of directed graphs, with the vertices being the number associated with the vertex. Here is an example with number of nodes $n=200$ (right-click and open in a new tab to see at proper resolution):

n = 200;
Graph[Table[i -> Mod[30 i, n, 1], {i, n}], ImageSize -> 1280,
VertexShape -> Table[i -> i, {i, n}], VertexSize -> 1,
VertexStyle -> White, EdgeStyle -> Green, Background -> Black]


It looks fine. Now let's try $n=5$:

n = 5;
Graph[Table[i -> Mod[30 i, n, 1], {i, n}], ImageSize -> 1280,
VertexShape -> Table[i -> i, {i, n}], VertexSize -> 1,
VertexStyle -> White, EdgeStyle -> Green, Background -> Black]


The vertex labels are gigantic and distort the figure to make it unreadable. Fine, let's just set VertexSize -> Automatic:

Much better. Let's try using VertexSize -> Automatic for the $n=200$ case once more:

The three-digit labels are unreadable, because Automatic does not appear to correctly scale them, and so it only displays the first digit and cuts off the rest with a ....

Is there an automatic way (other than VertexSize -> Automatic) to make text vertices show up as the right size without being truncated?

• Renders fine for me, 9.0.1, Windows, using VertexSize->Automatic
– ciao
Jun 14 '14 at 2:02
• I would need of this software to generate such random numbers on each vertice. Please , what is the name of software?
– user42779
Sep 1 '16 at 19:35
• If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Sep 1 '16 at 20:15
• This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review Sep 1 '16 at 21:41

I can see the same problem in Mathematica version 8.04. What cured the display problem was the explicit specification of a VertexShapeFunction as follows:

n = 200;
Graph[Table[i -> Mod[30 i, n, 1], {i, n}], ImageSize -> 1280,
VertexShape -> Table[i -> i, {i, n}], VertexSize -> Automatic,
VertexStyle -> White, EdgeStyle -> Green, Background -> Black,
VertexShapeFunction -> (Text[#2, #1] &)]


You can add this last option for all Graphs instead of specifying it in every instance, by doing this:

SetOptions[Graph, VertexShapeFunction -> (Text[#2, #1] &)];


Then your original code with VertexSize -> Automatic should appear without the cut-off labels.

• VertexShape->"Name" will work as well. The explicit Table specification is not necessary. Aug 19 '14 at 5:03
• @FrancoisVanderseypen Thanks, you're right - I just copied that from the original question.
– Jens
Aug 19 '14 at 5:08

You could play with GraphLayout:

n=200;
Graph[Table[i->Mod[30 i,n,1],{i,n}],ImageSize->1280,VertexShape->Table[i->i,
{i,n}],VertexSize->1,VertexStyle->White,EdgeStyle->Green,
Background->Black,GraphLayout->"BalloonEmbedding"]


• Hi, and thanks for the post. This doesn't exactly answer my question, since setting $n=5$ in your code still gives the same problem; I was mainly looking for something which renders correctly for all $n$. That said, my issue is a minor aesthetic one, and I can always do the vertex sizes manually if needed, so if no one else posts anything I'll accept this answer. I tend to use the SpringElectricalEmbedding method, I hadn't seen the BalloonEmbedding you used, seems like an interesting alternative. Jun 14 '14 at 3:29