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'm looking for something similar to AbsoluteOptions but that can be evaluated inside the function. That is, I want to read the absolute settings of options specified in the current context. For example, instead of:

AbsoluteOptions[
 Plot[
  Sin[x]
  , {x, 0, 2 Pi}
  , PlotRange -> Automatic
  ]
 , PlotRange]

{PlotRange -> {{0., 6.28319}, {-1., 1.}}}

I would like to have something like

Plot[
 Sin[x]
 , {x, 0, 2 Pi}
 , PlotRange -> Automatic
 , Epilog -> Inset[
   "PlotRange is " <> ToString@AbsoluteOptions[PlotRange]
   , {4, 1}]
 ]

Mathematica graphics

is it possible to read the absolute settings for options associated with the current function environment?

share|improve this question
3  
Are you interested primarily in PlotRange, which is known to be difficult to get? –  Michael E2 Aug 8 at 15:27
1  
@Michael_E2 Yes, my current need is to have access to PlotRange, but the question seems interesting to me in general. –  rhermans Aug 8 at 15:30
2  
Why not Show[{#},Epilog->Inset[ToString@AbsoluteOptions[#,PlotRange],{4,1}]]&@Plot[Sin[‌​x],{x,0,2 Pi},PlotRange->Automatic] –  Karsten 7. Aug 8 at 15:31
1  
Even if possible, you may bite your tail. Just imagine appending an Imagesize to the Plot Label, which in turns increase the image size –  belisarius Aug 8 at 15:38
    
The suggestion by @Karsten_7 its interesting, but that's not strictly speaking inside the function. The problem is way more complicated I expected, among other things PlotRange for other plots such as ContourPlot returns only 2 parameters, when 3 are expected. –  rhermans Aug 8 at 16:08

2 Answers 2

up vote 6 down vote accepted

You can use DisplayFunction for this:

Plot[Sin[x], {x, 0, 2 Pi}, 
 DisplayFunction :> (Show[#, 
     Epilog -> 
      Inset["PlotRange is " <> ToString@PlotRange[#], {4, 1}]] &)]

Instead of buggy PlotRange and AbsoluteOptions you can use much more reliable plotRange function from this answer:

plotRange[plot : (_Graphics | _Graphics3D | _Graph)] := 
 Reap[NotebookDelete[
    First@{PrintTemporary[
       Show[plot, Axes -> True, Frame -> False, 
        Ticks -> ((Sow[{##}]; Automatic) &), 
        DisplayFunction -> Identity, PlotRangePadding -> None, 
        ImageSize -> 0]], FinishDynamic[]}]][[2, 1]]

Plot[Sin[x], {x, 0, 2 Pi}, 
 DisplayFunction :> (Show[#, 
     Epilog -> 
      Inset["PlotRange is " <> ToString@plotRange[#], {4, 1}]] &)]

plot

Speaking about ContourPlot, there is no way to extract the plot range for Z-direction because produced 2D Graphics object does not contain this information. But you can catch the call to Contours using the technique used in the plotRange function:

{pl, plRange} = 
  Reap[ContourPlot[x^2 - y^2, {x, -2, 2}, {y, -2, 2}, 
    Contours -> ((Sow[{##}]; {-1, 0, 1}) &)]];
plRange

{{{-4., 4., 10}}}

I do not know what the third number means but the first two correspond to the actual plot range in Z-direction as can be seen from Plot3D:

plotRange[Plot3D[x^2 - y^2, {x, -2, 2}, {y, -2, 2}]]

{{-2., 2.}, {-2., 2.}, {-4., 4.}}

share|improve this answer
2  
I believe I haven't used DisplayFunction since version 5.2. +1 for old-school! :-) –  Mr.Wizard Aug 8 at 20:45

Not sure how robust it is, but the following seems to work for PlotRange:

ClearAll[plt];
plt = Plot[Sin[x], {x, 0, 3 Pi}, PlotRange -> Automatic, 
           PlotLabel -> Dynamic@("PlotRange is " <> ToString@PlotRange[plt])]

and

ClearAll[plt1];
plt1 = Plot[Sin[x], {x, 0, 3 Pi}, PlotRange -> Automatic, 
          PlotLabel ->  Dynamic@("PlotRange is " <> 
                           ToString@(PlotRange /. AbsoluteOptions[plt1, PlotRange]))]

both give

enter image description here

Similarly,

ClearAll[plt2];
plt2 = Plot[Sin[x], {x, 0, 2 Pi}, PlotRange -> Automatic,
      Epilog -> Dynamic@Inset[
               Style["PlotRange is " <> ToString@PlotRange[plt2], 14], {4, 1}]]

and

 ClearAll[plt3];
 plt3 = Plot[Sin[x], {x, 0, 2 Pi}, PlotRange -> Automatic,
    Epilog -> Dynamic@Inset[
     Style["PlotRange is " <> ToString@(PlotRange /. AbsoluteOptions[plt3, PlotRange]),
             14], {4, 1}]]

both give

enter image description here

share|improve this answer
1  
This one is really creative!:) –  belisarius Aug 8 at 17:32
    
thank you @belisarius.. –  kguler Aug 8 at 18:10

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.