An equivalent function to matplotlib.mlab.bivariate_normal

2.8k Views Asked by At

As of Matplotlib 3.1, mlab.bivariate_normal is removed from matplotlib's library. I was wondering if there is a built-in class that does the same job elsewhere (or in matplotlib)? I searched for a bit but can't find it readily.

I realize that one can just copy the function here and use it but I was wondering if there is a built-in function that one can call.

1

There are 1 best solutions below

0
On

As suggested by @tmdavison, scipy.stats.multivariate_normal() can be used to replace matplotlib.mlab.bivariate_normal().

The call:

Z = matplotlib.mlab.bivariate_normal(X, Y, sig_x, sig_y, mu_x, mu_y, sig_xy)

becomes:

rv = scipy.stats.multivariate_normal([mu_x, mu_y], [[sig_x, sig_xy], [sig_xy, sig_y]])
Z = rv.pdf(np.dstack((X, Y)))

The matplotlib.mlab.bivariate_normal() function had default values for all arguments except X and Y so you may have to add some extra values. The sig_x and sig_y values defaulted to 1.0 and all other arguments defaulted to 0.0. So if you are replacing the call bivariate_normal(X, Y) it would become multivariate_normal([0, 0], [[1, 0], [0, 1]]).pdf(np.dstack((X, Y))).