问题描述
我有一组150x150px png图像,以及它们对应的一组(x,y)坐标.有没有办法在网格上绘制图像?例如,我正在寻找一种R或Python解决方案来创建类似以下内容的内容:
I have a set of 150x150px png images, and a set of (x, y) coordinates that they correspond to. Is there a way to plot the images on a grid? For example, I'm looking for an R or Python solution to create something like the following:
推荐答案
您可以通过实例化 AnnotationBbox (每个图像一次)来创建边界框您希望显示的内容;图像及其坐标将传递给构造函数.
You create a bounding box by instantiating AnnotationBbox--once for each imagethat you wish to display; the image and its coordinates are passed to the constructor.
代码显然对于两个图像是重复的,因此一旦将该块放入函数中,它的长度就不会像这里看起来的那样.
The code is obviously repetitive for the two images, so once that block is put in a function, it's not as long as it seems here.
import matplotlib.pyplot as PLT
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from matplotlib._png import read_png
fig = PLT.gcf()
fig.clf()
ax = PLT.subplot(111)
# add a first image
arr_hand = read_png('/path/to/this/image.png')
imagebox = OffsetImage(arr_hand, zoom=.1)
xy = [0.25, 0.45] # coordinates to position this image
ab = AnnotationBbox(imagebox, xy,
xybox=(30., -30.),
xycoords='data',
boxcoords="offset points")
ax.add_artist(ab)
# add second image
arr_vic = read_png('/path/to/this/image2.png')
imagebox = OffsetImage(arr_vic, zoom=.1)
xy = [.6, .3] # coordinates to position 2nd image
ab = AnnotationBbox(imagebox, xy,
xybox=(30, -30),
xycoords='data',
boxcoords="offset points")
ax.add_artist(ab)
# rest is just standard matplotlib boilerplate
ax.grid(True)
PLT.draw()
PLT.show()
这篇关于将自定义图像放置在绘图窗口中-作为自定义数据标记或注释这些标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!