# 需要導入模塊: import matplotlib [as 別名]
# 或者: from matplotlib import offsetbox [as 別名]
def plot_tsne(X, imgs, outFile):
def imscatter(x, y, images, ax=None, zoom=1.0):
if ax is None:
ax = plt.gca()
x, y = np.atleast_1d(x, y)
artists = []
for x0, y0, img0 in zip(x, y, images):
im = OffsetImage(img0, zoom=zoom)
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=True)
artists.append(ax.add_artist(ab))
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
return artists
def plot_embedding(X, imgs, title=None):
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)
plt.figure()
ax = plt.subplot(111)
for i in range(X.shape[0]):
plt.text(X[i, 0], X[i, 1], ".", fontdict={'weight': 'bold', 'size': 9})
if hasattr(offsetbox, 'AnnotationBbox'):
imscatter(X[:,0], X[:,1], imgs, zoom=0.3, ax=ax)
plt.xticks([]), plt.yticks([])
if title is not None:
plt.title(title, fontsize=18)
tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
X_tsne = tsne.fit_transform(X)
plot_embedding(X_tsne, imgs, "t-SNE embeddings")
if outFile is None:
plt.show()
else:
plt.savefig(outFile, bbox_inches='tight')
plt.close()
# Plot image reconstructions