Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am puzzled by how AbsoluteOptions works with Graph objects. I would have expected that one could use it to obtain all the options used to draw a graph. There are several ways in which it behaves contrary to my expectations.

First, let' s make a simple graph, g, and set some custom options:

g = RandomGraph[{6, 11}, VertexStyle -> {1 -> Red, 2 -> Green}, 
    VertexSize -> {1 -> Large, 2 -> Large}, VertexLabels -> "Name", 
    ImagePadding -> 15, EdgeLabels -> "Name", Axes -> True]

g

1 - Why won' t all the options work again?

One expects that the options used for g could be obtained through AbsoluteOptions[g] and redeployed. However, this does not work. Either the code below locks up completely or the Out cell has a tooltip: "$Failed is not a Graphics primitive or directive".

allOptions = AbsoluteOptions[g];
g2 = Graph[EdgeList[g], allOptions]

2 - Some options work as expected, others do not. Why?

Let's take a subset of the options of g; namely, {VertexCoordinates, EdgeLabels, Axes, VertexLabels, VertexSize, AlignmentPoint, ImagePadding}. Now let's compare g and the partial replication, g3, side by side:

someOptions = 
  Sort@AbsoluteOptions[
    g, {VertexCoordinates, EdgeLabels, Axes, VertexLabels, VertexSize, 
    AlignmentPoint, ImagePadding}]
g;
g3 = Graph[EdgeList[g], someOptions]
someOptions3 = 
     Sort@AbsoluteOptions[
     g3, {VertexCoordinates, EdgeLabels, Axes, VertexLabels, VertexSize,
     AlignmentPoint, ImagePadding}]

(* Out someOptions3*)
{AlignmentPoint -> Center, Axes -> {True, True}, 
   EdgeLabels -> {"Name"}, ImagePadding -> 15., 
   VertexCoordinates -> {{0.518253, 0.817802}, {1.61587, 
   0.866512}, {2.05896, 0.336143}, {1.03973, 1.21024}, {1.04431, 0.}, {0., 0.377097}},      
VertexLabels -> {"Name"}, 
VertexSize -> {1 -> Large, 2 -> Large}}

g and g3

The vertex sizes are the same in each case, as expected. Also, the VertexStyle is different. That's fine: g3 has the default vertex style.

But why are the vertices in different locations? After all, we passed the VertexCoordinates option to g3. And the vertex coordinates returned by AbsoluteOptions[g3... do not correspond to the coordinates used for the vertices.

3 - What are failed options?

If we inspect the options used by g, we will notice that two of them failed, even though g was drawn with no apparent issues.

AbsoluteOptions[g, {VertexShapeFunction, EdgeShapeFunction}]

(* Out *)
{VertexShapeFunction -> $Failed, EdgeShapeFunction -> $Failed}

If you look at the graph, g, you see that the default vertex shapes and edge shapes were correctly rendered? So, whence the fail?

4 - Why is it insufficient to remove failed options?

If we remove the failed options and try to implement all the other options, MMA does one of two things: (1) It returns the unparsed Graph command or (2) it produces only a small portion of the graph, g.

Any help would be appreciated. I'm not trying to correct a particular program but rather understand the strengths and limitations of AbsoluteOptions.

share|improve this question
3  
AbsoluteOptions is hit and miss for many functions. You'll find options not recognized, options that are recognized but do not return the correct values, options that are recognized but only return correct values sometimes, i.e. conflicts between the option you want and other option values cause it to fail, ...and so on. Wolfram tech support have acknowledged that it is a flawed function. –  Mike Honeychurch Apr 21 '12 at 22:56
    
@MikeHoneychurch That verdict certainly matches my experience. –  David Carraher Apr 21 '12 at 23:24

1 Answer 1

up vote 4 down vote accepted

The difference in layout between g and g3 can be explained from the fact that their VertexLists are different:

g = RandomGraph[{6, 11}, VertexStyle -> {1 -> Red, 2 -> Green}, 
  VertexSize -> {1 -> Large, 2 -> Large}, VertexLabels -> "Name", 
  ImagePadding -> 15, EdgeLabels -> "Name"];

VertexList[g]

(* ==> {1, 2, 3, 4, 5, 6} *)

g3 = Graph[EdgeList[g], someOptions];
VertexList[g3]

VertexList[g3]

(* ==> {1, 6, 3, 5, 2, 4} *)

Try for example

{g, Graph[VertexList[g], EdgeList[g], someOptions]}

Mathematica graphics

share|improve this answer
    
Nice. That explains one big part of the mystery. –  David Carraher Apr 21 '12 at 17:57
    
Namely, question 2. However mostOptions = DeleteCases[allOptions, x_ /; MemberQ[{VertexShapeFunction -> $Failed , EdgeShapeFunction -> $Failed}, x]]; Graph[VertexList[g], EdgeList[g], mostOptions] still does not work. –  David Carraher Apr 21 '12 at 18:04
    
Try removing the options PlotRange and AspectRatio from someOptions. It seems that AbsoluteOptions doesn't return the right plot range when PlotRange is set to All. –  Heike Apr 21 '12 at 18:38
    
You're right. Also, Ticks gives incorrect values so it can not be relied on. –  David Carraher Apr 21 '12 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.