在绘图窗口中放置自定义图像 - 作为自定义数据标记或注释这些标记

gra*_*tur 28 python plot r matplotlib

我有一组150x150px的png图像,以及它们对应的一组(x,y)坐标.有没有办法在网格上绘制图像?例如,我正在寻找一个R或Python解决方案来创建如下内容: 在此输入图像描述

dou*_*oug 30

您可以通过为要显示的每个图像实例化AnnotationBbox --once来创建边界框; 图像及其坐标传递给构造函数.

对于这两个图像,代码显然是重复的,所以一旦将该块放入函数中,它就不会像这里看到的那样长.

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Pao*_*olo 17

在R(2.11.0和更高版本)中执行此操作的一种方法:

library("png")
# read a sample file (R logo)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
# img2 <- readPNG(system.file("img", "Rlogo.png", package="png"))
img2 <- readPNG("hand.png", TRUE) # here import a different image 
if (exists("rasterImage")) { 
  plot(1:1000, type='n')
  rasterImage(img, 100, 100, 200, 200)
  rasterImage(img2, 300, 300, 400, 400)
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅?readPNG和?rasterImage. 在此输入图像描述