5
$\begingroup$

We can have this Graph:

v = {1 \[UndirectedEdge] 9, 1 \[UndirectedEdge] 10, 
1 \[UndirectedEdge] 8, 10 \[UndirectedEdge] 2, 
10 \[UndirectedEdge] 5, 9 \[UndirectedEdge] 3, 
9 \[UndirectedEdge] 6, 8 \[UndirectedEdge] 7, 
8 \[UndirectedEdge] 4, 7 \[UndirectedEdge] 3, 
7 \[UndirectedEdge] 2, 6 \[UndirectedEdge] 2, 
6 \[UndirectedEdge] 4, 5 \[UndirectedEdge] 3, 
5 \[UndirectedEdge] 4};
g = Graph[v, VertexLabels -> Placed["Name", Center], 
VertexStyle -> White, VertexSize -> 0.5]

Which looks like this:

enter image description here

How can I add other characteristics inside the vertex labels?Such as other numbers in second line of labels?Or more info such as some codes in third line?

$\endgroup$
3
  • $\begingroup$ I would suggest to change the title: the title talks about VertexStyle while the question deals with VertexLabel. Or am I wrong $\endgroup$
    – Öskå
    Oct 2, 2013 at 16:52
  • $\begingroup$ @Öskå Both could be.Thanks for the offer but still I prefer to stand on my first title $\endgroup$
    – Alex
    Oct 2, 2013 at 17:06
  • $\begingroup$ This is really a question about vertices with multi-labels and it is a very common thing in network graphs. @Pinguin Dirk I would like to see a few more answers, if possible, with GraphPlot and VertexRenderingFunction. Thank you $\endgroup$ Mar 5, 2016 at 21:42

3 Answers 3

4
$\begingroup$

I am sure there must be an easier way (I cannot see at the moment), but it is possible to use VertexLabels the way you want:

labs = CharacterRange["a", "j"];
g = Graph[v, 
       VertexLabels -> (With[{vtx = Range@VertexCount@Graph@v}, 
          Rule @@@ Transpose[{vtx, Placed[ToString@# <> "\n" <> labs[[#]] "\nsome text", 
          Center] & /@ vtx}]]),
       VertexStyle -> White,
       VertexSize -> 0.75]

So, I just count the vertices (vtx) and set up a Rule to format the labels. Note that I used labs as to show how we can use labels that change from vertex to vertex. I hope this is what you had in mind:

enter image description here

Also, you might want to look into GraphPlot, and into VertexRenderingFunction in particular.

$\endgroup$
7
$\begingroup$

You could use Labeled. Because 2 of the 3 elements are placed outside the vertex, you can use smaller vertices.

Graph[{ Labeled[1, Placed[{1, "New York", "USA"}, {Center, Above, Below}]], 
        Labeled[2, Placed[{2, "Paris", "France"}, {Center, Above, Below}]], 
        Labeled[3, Placed[{3, "Rome", "Italy"}, {Center, Above, Below}]]}, 
      {1 -> 2, 2 -> 3, 3 -> 1},
 VertexLabelStyle -> 14, VertexStyle -> White, VertexSize -> 0.1, ImagePadding -> 30]

VertexLabels

$\endgroup$
12
  • $\begingroup$ Are you on Mathematica 9? $\endgroup$
    – Öskå
    Oct 2, 2013 at 17:27
  • $\begingroup$ Yes. I'm on Mathemaica 9. But Labeled does not required 9. $\endgroup$
    – DavidC
    Oct 2, 2013 at 17:29
  • $\begingroup$ I know. But I was wondering why your Graph started on the bottom left while mine started on the right :) So I guess it's some versions differences. $\endgroup$
    – Öskå
    Oct 2, 2013 at 17:31
  • $\begingroup$ On my machine, your code places the vertices in the same positions as my code. So, if you are using v 9, I can't explain the discrepancy. $\endgroup$
    – DavidC
    Oct 2, 2013 at 17:33
  • $\begingroup$ And on mine, your code places the vertices on the same positions as mine ;-) Hence the version question. $\endgroup$
    – Öskå
    Oct 2, 2013 at 17:35
3
$\begingroup$

Quite similar to Pinguin Dirk's answer, but here it is:

v = {1 \[UndirectedEdge] 9, 1 \[UndirectedEdge] 10, 
     1 \[UndirectedEdge] 8, 10 \[UndirectedEdge] 2, 
     10 \[UndirectedEdge] 5, 9 \[UndirectedEdge] 3, 
     9 \[UndirectedEdge] 6, 8 \[UndirectedEdge] 7, 
     8 \[UndirectedEdge] 4, 7 \[UndirectedEdge] 3, 
     7 \[UndirectedEdge] 2, 6 \[UndirectedEdge] 2, 
     6 \[UndirectedEdge] 4, 5 \[UndirectedEdge] 3, 
     5 \[UndirectedEdge] 4};

vertexLabels = Table[n -> Placed[Column[{n, "foo"}, Center], Center],
  {n, VertexCount[Graph[v]]}];

g = Graph[v, VertexLabels -> vertexLabels, VertexStyle -> White, VertexSize -> 0.5]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ The problem with your answer & Pinguin Dirk's is that the OP would not really be able to say anything else than foo or some text. Plus, the OP would not be able to choose whether he wants more than one line or not. $\endgroup$
    – Öskå
    Oct 2, 2013 at 17:17

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.