# Density plot with dots

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:

How to do it?

## 1 Answer

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]
}]


• OP seems to want an arbitrary function. – Kuba Jan 26 '18 at 8: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 8:09
• @b3m2a1 done I guess: mathematica.stackexchange.com/a/32540/5478 – Kuba Jan 26 '18 at 8: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 8:15
• @b3m2a1 yep, that was my point :) – Kuba Jan 26 '18 at 8:17