Suppose we have a hard to calculate function f. We plot it like e.g.


After some long calculation this gives us a nice image, it can be rotated and all, but suddenly we want to look at a density plot of f. We can of course repeat the whole long calculation with DensityPlot, but it's wasteful. We could instead have pre-calculated a table of values of f using Table, but this would imply homogeneous (or some manually-parametrized) grid, which can be an inefficient (or hard to implement) choice for many functions.

So what is needed is a way to sample the function as e.g. Plot3D would do it with given MaxRecursion and PlotPoints, but instead of putting the result into a graphics box, put it into a variable as a list of coordinates or whatever form suitable for List*Plot* family of functions. How can this be done?

  • $\begingroup$ Make an appromixation, e.g. f2 = FunctionInterpolation[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}] and use that in the plots. $\endgroup$
    – Coolwater
    Commented Sep 3, 2016 at 21:05
  • $\begingroup$ @Coolwater but FunctionInterpolation doesn't seem to have controls over how it samples the function. $\endgroup$
    – Ruslan
    Commented Sep 3, 2016 at 21:07
  • $\begingroup$ Take a look here: mathematica.stackexchange.com/q/216/12 and here: mathematica.stackexchange.com/a/1011/12 I recommend the second. The first has issues. I have a much more complex solution to the first one which unfortunately was too messy and too involved to post here ... $\endgroup$
    – Szabolcs
    Commented Sep 3, 2016 at 22:13
  • $\begingroup$ The title would seem to be duplicate of these: mathematica.stackexchange.com/questions/82912/…, mathematica.stackexchange.com/questions/88299/… -- maybe indicate it's higher dimensional sampling or 2D sampling that you're interested in, whichever is more accurate? $\endgroup$
    – Michael E2
    Commented Sep 3, 2016 at 22:39

1 Answer 1


Say for example :

 gr00 = Plot3D[
            Sin[x y],
            {x, -5, 5}, {y, -5, 5},
            MaxRecursion -> 5, PlotPoints -> 30, PlotRange -> All]  

enter image description here

The data, including the mesh, are the first element of GraphicsComplex[...] in gr00 :

gr00 // InputForm // Short  

enter image description here

Then :

ListDensityPlot[gr00[[1, 1]]]  

enter image description here

ListDensityPlot[gr00[[1, 1]], Mesh -> All]  

enter image description here

You can even do a copy-paste of your graphic inside the ListDensityPlot, the data are not lost :

enter image description here

--> same result as above

  • 2
    $\begingroup$ I like this, but it does depend on the internal, undocumented structure of an object $\endgroup$
    – mikado
    Commented Sep 4, 2016 at 5:27
  • 1
    $\begingroup$ @mikado The doc of Plot3D says : "Plot3D returns GraphicsComplex[data]]" without saying what is exactly data. You can use a more robust extraction of data with something like Cases[...,GraphicsComplex[___],{1,Infinity}]. The philosophy not do mix data and presentation (tick...) is stable on mathematica since the beginning. I think that the main risk is to have the Graphics[...] form instead of GraphicsComplex (see Normal[ComplexExpand[..]]), because GraphicsComplex[] was only introduced in 2007. So effectively, I only use this is interactive sessions. $\endgroup$
    – andre314
    Commented Sep 4, 2016 at 9:48

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.