def plot_embedding_contours(Y, contours = 10, cmap = plt.cm.plasma, xmin = None, xmax = None, ymin = None, ymax = None, npts = 100, density = False):
"""Plot a 2d density map of the embedding Y"""
if xmin is None:
xmin = np.min(Y[:,0]);
if xmax is None:
xmax = np.max(Y[:,0]);
if ymin is None:
ymin = np.min(Y[:,1]);
if ymax is None:
ymax = np.max(Y[:,1]);
#print xmin,xmax,ymin,ymax
dx = float(xmax-xmin) / npts;
dy = float(xmax-xmin) / npts;
xx, yy = np.mgrid[xmin:xmax:dx, ymin:ymax:dy]
positions = np.vstack([xx.ravel(), yy.ravel()])
kernel = st.gaussian_kde(Y.T);
#print xx.shape
#print positions.shape
#print Y.shape
#print kernel(positions).shape
f = kernel(positions)
f = np.reshape(f, xx.shape)
#print f.shape
ax = plt.gca()
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
# Contourf plot
if density:
cfset = ax.contourf(xx, yy, f, cmap=cmap)
## Or kernel density estimate plot instead of the contourf plot
#ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax])
# Contour plot
if contours is not None:
cset = ax.contour(xx, yy, f, contours, cmap=cmap)
# Label plot
#ax.clabel(cset, inline=1, fontsize=10)
ax.set_xlabel('Y0')
ax.set_ylabel('Y1')
评论列表
文章目录