5
$\begingroup$

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?

$\endgroup$

3 Answers 3

5
$\begingroup$
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]

Mathematica graphics

compare to

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

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Thanks! It works. I really need to learn more about Reap function. $\endgroup$
    – user39954
    Jul 11, 2014 at 3:31
  • $\begingroup$ 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 ? $\endgroup$
    – sekisushai
    Jul 13, 2016 at 22:36
  • $\begingroup$ @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 $\endgroup$
    – Nasser
    Jul 13, 2016 at 23:59
  • $\begingroup$ @Nasser yes, unfortunately DensityPlot cannot return easily the values on the grid with the plot itself. $\endgroup$
    – sekisushai
    Jul 15, 2016 at 16:14
2
$\begingroup$

how about this?

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

ListDensityPlot[lists, Mesh -> None]

enter image description here

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]
$\endgroup$
2
  • $\begingroup$ Thank you. So Table[] divides the domains {x,0,2pi} and {y,0,2pi} automatically? $\endgroup$
    – user39954
    Jul 11, 2014 at 3:42
  • $\begingroup$ Automatical dividing number is '1', but If you make more dividing point, then do like this. $\endgroup$
    – Junho Lee
    Jul 11, 2014 at 5:32
2
$\begingroup$

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]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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