2

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?

  • This topic is slightly different but one of answers fully answer a general case: https://mathematica.stackexchange.com/a/32540/5478 – Kuba Jan 26 '18 at 08:15

1 Answers1

2

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:

Graphics[{
    Opacity[.05],
    Point @ RandomVariate[dist, 10^5]
}]

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 1
    OP seems to want an arbitrary function. – Kuba Jan 26 '18 at 08:08
  • 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. – b3m2a1 Jan 26 '18 at 08:09
  • 1
    @b3m2a1 done I guess: https://mathematica.stackexchange.com/a/32540/5478 – Kuba Jan 26 '18 at 08:13
  • @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. – b3m2a1 Jan 26 '18 at 08:15
  • 1
    @b3m2a1 yep, that was my point :) – Kuba Jan 26 '18 at 08:17
  • @b3m2a1 and Carl, I am not sure what should I do. Clearly ybeltukov crossed the line by posting too general answer! :) It is not an exact duplicate but 'this is answerd in ..' description fits. I'd close it then and leave it as a roadsign. Or? CW answer with ybeltukov's Metropolis? – Kuba Jan 26 '18 at 08:20
  • I'd do the latter. It's worth keeping this open as it's not a dupe question and other people might have other ideas. So I think CW-ing ybeltukov's answer (with a link of course) is the way to go. – b3m2a1 Jan 26 '18 at 08:22
  • @Kuba Sounds like a duplicate to me. Could reopen if the OP adds details explaining why the other answer doesn't apply. – Carl Woll Jan 26 '18 at 08:24