Search code examples
pythongaussian

How to generate 2D gaussian with Python?


I can generate Gaussian data with random.gauss(mu, sigma) function, but how can I generate 2D gaussian? Is there any function like that?


Solution

  • Since the standard 2D Gaussian distribution is just the product of two 1D Gaussian distribution, if there are no correlation between the two axes (i.e. the covariant matrix is diagonal), just call random.gauss twice.

    def gauss_2d(mu, sigma):
        x = random.gauss(mu, sigma)
        y = random.gauss(mu, sigma)
        return (x, y)