# Obtain data points from a density plot

I have a density plot, for example

DensityPlot[Sin[x*y],{x,0,2pi},{y,0,2pi}].


Then I would like to collect points from this densityplot into a file so that I can always use ListDensityPlot to draw these points again, or I may draw these points by some other software.

I read several posts on how to do that for Plot, but how can I generalize them to DensityPlot?

data = Reap[DensityPlot[Sin[x*y], {x, 0, 2 Pi}, {y, 0, 2 Pi},
EvaluationMonitor :> Sow[{x, y, Sin[x*y]}]]][[2, 1]];

ListPlot3D[data]


compare to

 DensityPlot[Sin[x*y], {x, 0, 2 Pi}, {y, 0, 2 Pi}]


• Thanks! It works. I really need to learn more about Reap function. – user39954 Jul 11 '14 at 3:31
• For each point {x,y} your solution evaluate the function f[x,y]' to DensityPlot twice: one time for the DensityPlot value itself, and one for the Sow, as you put back f[x,y] in Sow. Is there a way to call f[x,y] on time only ? – Pierre L. Jul 13 '16 at 22:36
• @PierreL. the only way to evaluate only once, that I know about, is to generate the data off-line using Table command, then use DensityPlot but the reason for doing the above, is to use the same sampling as that used by DensityPlot which can be different and not uniform as what one might pick using Table. Sometimes Plot uses an adaptive sampling. I – Nasser Jul 13 '16 at 23:59
• @Nasser yes, unfortunately DensityPlot cannot return easily the values on the grid with the plot itself. – Pierre L. Jul 15 '16 at 16:14

lists = Table[
Sin[x*y], {x, 0, 2 \[Pi], \[Pi]/20}, {y, 0, 2 \[Pi], \[Pi]/20}];

ListDensityPlot[lists, Mesh -> None]


Or like this

lists = DensityPlot[Sin[x*y], {x, 0, 2 \[Pi]}, {y, 0, 2 \[Pi]}][[1,
1]];

ListDensityPlot[{#1, #2, Sin[#1 #2]} & @@@ lists, Mesh -> None]

• Thank you. So Table[] divides the domains {x,0,2pi} and {y,0,2pi} automatically? – user39954 Jul 11 '14 at 3:42
• Automatical dividing number is '1', but If you make more dividing point, then do like this. – Junho Lee Jul 11 '14 at 5:32

Though DensityPlot only contains color info for z value.

However, you could obtain data from equivalent Plot3D

plot=Plot3D[Sin[x*y], {x, 0, 2 Pi}, {y, 0, 2 Pi}]


If you see the plot//FullForm, you can find point data contained in GraphicsComplex[list of data,__]

So to extract data, it is as simple as

plot[[1,1]]


You can check this by

ListDensityPlot[plot[[1,1]]]
`