Graph with VertexLabels and formatting function not working

General discussion about Mathematica features and functionality...
Forum Rules
By using the Wolfram Faculty Program Forum, you agree not to post any abusive, obscene, vulgar, slanderous, hateful, threatening, or sexually oriented material. Wolfram Faculty Program Forum administrators have the right to remove, edit, move or close any topic at any time should we see fit.

Personal Information: Posts in this forum may be viewed by non-members; however, the forum prohibits non-members from viewing your profile. Although your email address is hidden from both non-members and members, your account is initially configured to allow members to contact you via email through the forum. If you wish to hide your profile, or prohibit others from contacting you directly, you may change these settings by updating your profile through the User Control Panel.

Attachments: Attachments are not currently enabled on this forum. To share a file with others on this site, simply upload your file to the online storage service of your choice and include a link to the file within your post. If your school does not offer an online file storage and sharing service, the following sites provide free basic online file storage and sharing: Mozy, FilesAnywhere, Adrive, and KeepandShare.

Graph with VertexLabels and formatting function not working

Postby Tom_Verhoeff » Thu May 30, 2013 9:28 pm

In Mathematica 9, the built-in graph facility allows you to control the rendering of vertex labels through the option VertexLabels. E.g.
Code: Select all
VertexLabels -> Placed["Name",Tooltip]
shows the vertex itself as vertex label in a tooltip when the mouse is hovered over the vertex.

According to the documentation you can also apply your own formatting function f to the label, by providing f as a third argument to Placed. However, in the examples with VertexLabels, the vertex labels in that case are forced to some constant, rather than using "Name" to have the formatting function applied to the vertices as defined in the graph. That is, the code looks like
Code: Select all
Placed["xxx",Center,rotateLabel]
, which appears to work (rendering each label as xxx). But
Code: Select all
Placed["Name",Center,rotateLabel]
does not work as expected; rotateLabel is not applied to the vertex labels defined in the graph, but rather to the string literal "Name".

A cumbersome way out seems to be to enumerate all vertices and explicitly define the labels in the VertexLabels option as the vertex itself, like
Code: Select all
Graph[vertices,VertexLabels->Map[#->Placed[rotateLabel[#],Center]&,vertices]
. But this seems to me to be a waste of code.

By the way,
Code: Select all
Graph[vertices,VertexLabels->Map[#->Placed[#,Center,rotateLabel]&,vertices]
does not work as expected.

In my application, the vertices are actually permutations in the form of lists of numbers.

Are these shortcomings of Mathematica, or am I doing something wrong?

Thanks,

Tom
User avatar
Tom_Verhoeff
 
Posts: 3
Joined: Wed May 29, 2013 7:29 pm
Organization: Eindhoven University of Technology
Department: Mathematics and Computer Science

Re: Graph with VertexLabels and formatting function not working

Postby Tom_Verhoeff » Sat Jun 01, 2013 10:53 am

Here is some concrete code to illustrate my question.

The following code works. The vertices are numbers, and these numbers are used as labels, implicitly through "Name".
Code: Select all
Block[{
  vertices = Range[6],
  f = Divisible[#1, #2] || Divisible[#2, #1] &,
  edges},
edges = Select[Subsets[vertices, {2}], f @@ # &];

System`Graph[
  vertices,
  Map[UndirectedEdge @@ # &, edges],
  VertexSize -> .2,
  VertexLabels -> Placed["Name", Center]
  ]
]


If you want to apply custom formatting to the labels, then the following might be expected to work according to the documentation, but it does not work:
Code: Select all
Block[{
  vertices = Range[6],
  f = Divisible[#1, #2] || Divisible[#2, #1] &,
  edges},
edges = Select[Subsets[vertices, {2}], f @@ # &];

System`Graph[
  vertices,
  Map[UndirectedEdge @@ # &, edges],
  VertexShape -> None,
  VertexLabels -> Placed["Name", Center,Framed[#,Background->LightYellow]&]
  ]
]


Instead, you need to attach labels explicitly to each vertex:
Block[{
vertices = Range[6],
f = Divisible[#1, #2] || Divisible[#2, #1] &,
edges},
edges = Select[Subsets[vertices, {2}], f @@ # &];

System`Graph[
vertices,
Map[UndirectedEdge @@ # &, edges],
VertexShape -> None,
VertexLabels ->
Map[# -> Placed[#, Center, Framed[#, Background -> LightYellow] &] &,
vertices]
]
]
User avatar
Tom_Verhoeff
 
Posts: 3
Joined: Wed May 29, 2013 7:29 pm
Organization: Eindhoven University of Technology
Department: Mathematics and Computer Science


Return to General Mathematica Discussion

Who is online

Users browsing this forum: No registered users and 1 guest