问题描述
我有一组 150x150 像素的 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()
这篇关于在绘图窗口中放置自定义图像——作为自定义数据标记或注释这些标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!