使用带有彩色框架的matplotlib在绘图中显示图像 [英] Show images in a plot using matplotlib with a coloured frame
问题描述
我想绘制一个带有图像的图形.我正在进行k均值聚类,然后,我要在其聚类中以相同的框架颜色显示每个图像.
I want to plot a graph with images on it. I am doing k-means clustering and after that, I want to show each image on its cluster with the same frame colour.
我有一些代码,基本上将图像放在带有黑框的图形上
I have some code which basically places images on the graph with a black frame
fig = plt.gcf()
fig.clf()
ax = plt.subplot(111)
# add a first image
for i in range(0, len(dataset['val'].path)):
ab = AnnotationBbox(OffsetImage(img, zoom=.15, cmap='gray'),
[reduced_data[i][0], reduced_data[i][1]],
frameon=True,
xybox=(10, 10),
xycoords='data',
boxcoords="offset points",
arrowprops=dict(arrowstyle="-"))
ax.add_artist(ab)
plt.draw()
plt.show()
我正在检查 scickit学习
文档中的一些教程,还检查了 matplotlib
中的 AnnotationBbox
和 OffsetImage
构造函数代码>网页,没有结果.我想知道是否有一种方法可以更改插入绘图中图像的框架颜色,以使其与我为每个聚类提供的颜色匹配.
I was checking some tutorials on the scickit learn
documentation and also I checked the AnnotationBbox
and OffsetImage
constructors in matplotlib
webpage with no results. I was wondering if there is a way to change the frame colour of the images inserted in the plot so it matches the color I am giving to each cluster.
推荐答案
您可以使用 bboxprops = dict(edgecolor ='red')
设置AnnotationBbox的边框.一个最小的例子,
You can set the bounding box of your AnnotationBbox using bboxprops=dict(edgecolor='red')
.
A minimal example,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
fig, ax = plt.subplots()
im = OffsetImage(np.arange(100).reshape((10, 10)))
ab1 = AnnotationBbox(im, (0.5, 0.5),
bboxprops =dict(edgecolor='red'))
ab2 = AnnotationBbox(im, (0.75, 0.75),
bboxprops =dict(edgecolor=[0.2,1.,0.5] ))
ax.add_artist(ab1)
ax.add_artist(ab2)
plt.show()
给出,
这篇关于使用带有彩色框架的matplotlib在绘图中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!