Skip to content

Commit 1f3e760

Browse files
committed
added 2d kde plot and made kde available as stand alone plot
1 parent 757208c commit 1f3e760

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

pymc/plots.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
from scipy.stats import kde
44

5-
__all__ = ['traceplot']
5+
__all__ = ['traceplot', 'kdeplot', 'kde2plot']
66

77
def traceplot(trace, vars=None):
88
if vars is None:
@@ -14,7 +14,7 @@ def traceplot(trace, vars=None):
1414
for i,v in enumerate(vars):
1515
d = np.squeeze(trace[v])
1616

17-
kdeplot(ax[0,i], d)
17+
kdeplot_op(ax[0,i], d)
1818
ax[0,i].set_title(str(v))
1919
ax[1,i].plot(d, alpha = .35)
2020

@@ -23,7 +23,7 @@ def traceplot(trace, vars=None):
2323

2424
return f
2525

26-
def kdeplot(ax, data):
26+
def kdeplot_op(ax, data):
2727
data = np.atleast_2d(data.T).T
2828
for i in range(data.shape[1]):
2929
d = data[:,i]
@@ -33,6 +33,32 @@ def kdeplot(ax, data):
3333
x = np.linspace(0,1,100)*(u-l)+l
3434

3535
ax.plot(x,density(x))
36+
37+
def kde2plot_op(ax, x, y, grid = 200):
38+
xmin = x.min()
39+
xmax = x.max()
40+
ymin = y.min()
41+
ymax = y.max()
42+
43+
grid = grid*1j
44+
X, Y = np.mgrid[xmin:xmax:grid, ymin:ymax:grid]
45+
positions = np.vstack([X.ravel(), Y.ravel()])
46+
values = np.vstack([x, y])
47+
kernel = kde.gaussian_kde(values)
48+
Z = np.reshape(kernel(positions).T, X.shape)
49+
50+
ax.imshow(np.rot90(Z), cmap=p.cm.gist_earth_r,
51+
extent=[xmin, xmax, ymin, ymax])
52+
53+
def kdeplot(data):
54+
f, ax = p.subplots(1, 1, squeeze = True)
55+
kdeplot_op(ax, data)
56+
return f
57+
58+
def kde2plot(x,y, grid = 200):
59+
f, ax = p.subplots(1, 1, squeeze = True)
60+
kde2plot_op(ax, x,y, grid)
61+
return f
3662

3763

3864

0 commit comments

Comments
 (0)