How can I label particular vertices in GraphPlot? And have the arrows space out accordingly, for the width of the label?

I have Mathematica 7, but I think this question applies to all versions.

Mathematica Graph Plot

Edit: I posted the above graph so you can see label overlap might kill whatever information the graph is displaying.

For simplicity, lets do a mock graph:

Generate the Data:

data = #[[1]] -> #[[2]] & /@ Partition[RandomInteger[10, 100], 2]

Plot the Graph:

GraphPlot[data, EdgeRenderingFunction -> ({Red, {Arrowheads[Small], Arrow[#1, .07]}} &)]

Gives something like:

Random GraphPlot

If I wanted to explicitly label "1" and "5", this is where I'm completely lost:

I'm happy with the ugly yellow boxes for now:

GraphPlot[data,EdgeRenderingFunction->({Red,{Arrowheads[Small],Arrow[#1,.07]}}&), VertexLabeling->True]

yellow box, on all labeling

But you can see it can be problematic if I labeled every point in the first graph. I wish the VertexLabelingFunction help pages was a bit more thorough, all it seem it do is some sort of rule based, rather than explicit labels. I'm thinking it'll be some VertexLabelingFunction -> (If[#1 == "1" || #1 == "5", Text[#1]])& But that always gives me an error.

  • 3
    $\begingroup$ Are you sure you want a mathematica solution and not one in ruby? ;) $\endgroup$
    – yoda
    Feb 10, 2012 at 1:49
  • $\begingroup$ Write custom vertex and edge rendering functions and pass them to GraphPlot. There are significant differences in the arguments to the VertexRenderingFunction and GraphRenderingFunction between Mathematica 7 and 8. Do you prefer an answer for 7 or for 8? $\endgroup$
    – cah
    Feb 10, 2012 at 3:37
  • 4
    $\begingroup$ Please recheck your understanding of "sexy" with a good doctor :). $\endgroup$ Feb 10, 2012 at 15:15
  • 1
    $\begingroup$ Perhaps you should include the code for a simple graph as an example. The GraphPlot above cannot be used by answerers and is therefore less useful as an illustration. By the way, a considerable proportion of the Mathematica community that stalked this tag has moved to the dedicated Mathematica site at Mathematica.stackexchange.com. You might want to consider moving as well. $\endgroup$ Feb 10, 2012 at 22:57
  • 1
    $\begingroup$ Have you tried VertexRenderingFunction? $\endgroup$ Feb 10, 2012 at 23:07

1 Answer 1


You could do something like this. It's an adaptation of the first example in the documentation for VertexLabelingFunction where I used an If statement to determine whether a vertex should be labeled or not. The function offset is just a helper function to determine by what amount the arrows should be shortened based on whether they end or start at a labeled vertex or not and lblLst is the list of vertices you want to label:

lblLst = {1, 5, 10};

offset[lblLst_, edge : {e1_, e2_}] := If[MemberQ[lblLst, #], .13, .07] & /@ edge

 EdgeRenderingFunction ->
  ({Red, Arrowheads[Small], Arrow[#1, offset[lblLst, #2]]} &),

 VertexRenderingFunction -> (If[MemberQ[lblLst, #2],
     {White, EdgeForm[Black], Disk[#, .1], Black, Text[#2, #1]},
     {Blue, Point[#1]}] &)]

Mathematica graphics

  • $\begingroup$ try this data(juggernautmotorsports.ca/work/code/mathematica/data) and you'll see what I mean. With lblLst = {"jshamess", "BuzzBuzzHome", "Urban_Toronto", "RealNet_Canada", "Urbanation", "BrianPersaud"}. To load into file, DL the data from link, then GraphPlot[<< "~/data", EdgeRe... if data is in your home dir, or <<"~/Downloads/data" if it's in your Downloads folder. Thanks so much though, it's a huge leap forward. $\endgroup$ Feb 11, 2012 at 19:00
  • $\begingroup$ How can you keep your Tooltip labeling using an Vertex Rendering Functinon? $\endgroup$
    – SumNeuron
    Sep 2, 2016 at 17:22

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.