forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauss_plot_2d.py
More file actions
53 lines (40 loc) · 1.22 KB
/
gauss_plot_2d.py
File metadata and controls
53 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
import os
figdir = "../figures"
def save_fig(fname): plt.savefig(os.path.join(figdir, fname))
from mpl_toolkits.mplot3d import axes3d
from scipy.stats import multivariate_normal
Gs = ["Full", "Diag", "Spherical"]
mu = [0, 0]
Covs = {'Full': [[2, 1.8], [1.8, 2]],
'Diag': [[1, 0], [0, 3]],
'Spherical': [[1, 0], [0, 1]]}
#Multivariate gaussian PDF
def Gpdf(x, y, G):
return multivariate_normal(mean=mu, cov=Covs[G]).pdf([x, y])
Gpdf = np.vectorize(Gpdf, excluded=['G'])
points = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(points, points)
def make_contour_plot(G):
Z = Gpdf(X, Y, G)
fig, ax = plt.subplots()
ax.contour(X, Y, Z)
plt.axis('equal')
plt.title(G)
plt.draw()
save_fig('gaussPlot2dDemoContour{}.pdf'.format(G))
plt.show()
def make_surface_plot(G):
Z = Gpdf(X, Y, G)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, color='white', edgecolor="black")
#ax.axis('equal')
#ax.title(G)f
plt.draw()
save_fig('gaussPlot2dDemoSurf{}.pdf'.format(G))
plt.show()
for g in Gs:
make_contour_plot(g)
make_surface_plot(g)