# Getting PlotPoints value from a Plot

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]

• Options[a,PlotRange]. – yulinlinyu Apr 25 '16 at 2:09
• 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. – yode Apr 25 '16 at 2:16
• 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. – Rashid Apr 25 '16 at 3:24
• 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. – Prashanth Apr 25 '16 at 4:06
• 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. – m_goldberg Apr 25 '16 at 4:56

## 1 Answer

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] ",
Chartingget2DPlotRange@plotcommand}};
plot
]


Here we are using Trace to find the actual values of MaxRecursion and PlotPoints used, and the undocumented function Chartingget2DPlotRange@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}]]


another example,

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


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]
Chartingget2DPlotRange@%


if you don't mind the duplicated results from TracePrint

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