python中next_offset_Python matplotlib.offsetbox方法代碼示例

该博客展示了如何在Python中使用matplotlib的offsetbox模块进行图像散点图的绘制。通过`imscatter`函数和`AnnotationBbox`,结合TSNE降维算法,将图像与二维坐标点对应展示,并提供了完整的代码示例。
摘要由CSDN通过智能技术生成

# 需要導入模塊: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值