3
$\begingroup$

I want to display the PlotPoints and MaxRecursion values used to plot the current graph so that the user can change the values relatively to improve the output. Is it possible to get the PlotPoints and MaxRecursion information from a plot?

a = 
  Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi}, 
    PlotPoints -> 10, MaxRecursion -> 2]

PlotRange[a]
PlotPoints[a]
MaxRecursion[a]
$\endgroup$
  • 1
    $\begingroup$ Options[a,PlotRange]. $\endgroup$ – yulinlinyu Apr 25 '16 at 2:09
  • $\begingroup$ AbsoluteOptions can help a little.You can get the value of PlotRange like this AbsoluteOptions[a, PlotRange]But the value of PlotPoints and MaxRecursion Maybe be vanished after you get the a. $\endgroup$ – yode Apr 25 '16 at 2:16
  • 1
    $\begingroup$ If the goal is to expose these values to users, you might try to pass them to the PlotLabel too. Or you could pass them to a combined Text graphic as shown in the 3rd example on howTo/MakeASmootherOrRougherPlot. This shows a dynamic example with labels for MaxRecursion and PlotPoints. $\endgroup$ – Rashid Apr 25 '16 at 3:24
  • $\begingroup$ my requirement is, to first plot the graph using Automatic. And if the user is not satisfied with the result, he could change the options. For the user to easily change the option values, I thought it would be good to show them the current values. $\endgroup$ – Prashanth Apr 25 '16 at 4:06
  • 2
    $\begingroup$ As far I know there is no way to get the values used for MaxRecursion or PlotPoints from the result of evaluating a Plot expression. $\endgroup$ – m_goldberg Apr 25 '16 at 4:56
7
$\begingroup$

You can do something like this,

SetAttributes[verbosePlot, HoldAll]
verbosePlot[plotcommand_] := Module[{plot, pp, mr},
  {pp, mr} = {PlotPoints, 
     MaxRecursion} /. (Trace[plot = plotcommand, 
         HoldPattern[(MaxRecursion -> _Integer) | (PlotPoints -> _Integer)],
         TraceInternal -> True] // Flatten // Reverse // 
      ReleaseHold);
  Print@*Row /@ {{"MaxRecursions \[Rule] ", 
     mr}, {"PlotPoints \[Rule] ", pp}, {"PlotRange \[Rule] ", 
     Charting`get2DPlotRange@plotcommand}};
  plot
  ]

Here we are using Trace to find the actual values of MaxRecursion and PlotPoints used, and the undocumented function Charting`get2DPlotRange@plotcommand to get the PlotRange (a different method is needed for this option since Trace will return PlotRange->All if that is the option given).

Thanks to Simon Woods for this method, and thanks to J.M. for the tips on improving it. This will plot the command and give the values for the requested option.

verbosePlot[Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi}]]

enter image description here

another example,

verbosePlot[
 ParametricPlot[
  r^2 { Sqrt[t] Cos[t], Sin[t]}, {t, 0, 3 Pi/2}, {r, 1, 2}]]

enter image description here

If you want to extract the option values from an already created plot, I don't know how to do that. The only information available in the FullForm of the plot would be the number of mesh points, not the algorithm used to generate them.

Note that you can also bypass this user-defined function and go straight to TracePrint,

TracePrint[
 ParametricPlot[
  r^2 {Sqrt[t] Cos[t], Sin[t]}, {t, 0, 3 Pi/2}, {r, 1, 
   2}], (MaxRecursion -> _Integer) | (PlotPoints -> _Integer), 
 TraceInternal -> True]
Charting`get2DPlotRange@%

enter image description here

if you don't mind the duplicated results from TracePrint

$\endgroup$
  • $\begingroup$ I would like to know how did you know the usage of the functions that located in context Charting. Thanks:) $\endgroup$ – xyz Apr 25 '16 at 8:55
  • $\begingroup$ I got Charting`get2DPlotRange and Charting`get3DPlotRange from hanging around here, very useful functions. I don't know what else is in the Charting` package though $\endgroup$ – Jason B. Apr 25 '16 at 8:57
  • $\begingroup$ @ShutaoTANG - but there do seem to be a lot of them: Names["Charting`*"] $\endgroup$ – Jason B. Apr 25 '16 at 8:58
  • $\begingroup$ In general, I using ?Charting` :) A few days ago, I saw this function in your answer $\endgroup$ – xyz Apr 25 '16 at 9:05
  • $\begingroup$ Ahhh, but these are undocumented functions, so you won't find any documentation that way :-D $\endgroup$ – Jason B. Apr 25 '16 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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