I want to plot an arbitrary 2D function, for example $ e ^ { -(x^2+y^2) } $. The plot should contain dots, the density of dots at a point indicating the value of the function at that point.

The output should be something like this: Sample plot

How to do it?


You could use RandomVariate on a BinormalDistribution:

dist = BinormalDistribution[{0,0}, {1,1}/Sqrt[2], 0];

Check the pdf:

PDF[dist, {x,y}] //Simplify

E^(-x^2 - y^2)/π

Up to normalization, it is the same as your function. Then, we can generate some random points and plot:

    Point @ RandomVariate[dist, 10^5]

enter image description here

  • 1
    $\begingroup$ OP seems to want an arbitrary function. $\endgroup$ – Kuba Jan 26 '18 at 8:08
  • $\begingroup$ I think you could use a Metropolis-sampling type algorithm to generate this for arbitrary functions (since basically we're doing a numerical approximation of the function but with delta functions) but I'm not expert enough to quickly vomit one of those out. $\endgroup$ – b3m2a1 Jan 26 '18 at 8:09
  • 1
    $\begingroup$ @b3m2a1 done I guess: mathematica.stackexchange.com/a/32540/5478 $\endgroup$ – Kuba Jan 26 '18 at 8:13
  • $\begingroup$ @Kuba good find! I'll definitely have to keep that one in mind. And if you look at like fig. 3 from that answer it's exactly what the OP wants. $\endgroup$ – b3m2a1 Jan 26 '18 at 8:15
  • 1
    $\begingroup$ @b3m2a1 yep, that was my point :) $\endgroup$ – Kuba Jan 26 '18 at 8:17

Your Answer

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