2011-02-01 15 views
25

Mam zestaw obrazów PNG 150x150px i zestaw współrzędnych (x, y), których odpowiadają. Czy istnieje sposób na wykreślenie obrazów na siatce? Na przykład, szukam rozwiązania R lub Python, aby stworzyć coś podobnego, co następuje: enter image description hereUmieszczanie obrazów niestandardowych w oknie wydruku - jako niestandardowych znaczników danych lub do oznaczania ich znaczników

+0

Powiązane: http://stackoverflow.com/questions/11487797/python-matplotlib-basemap-overlay-small-image-on-map-plot –

Odpowiedz

27

utworzyć obwiednię przez instancji AnnotationBbox --once dla każdego obrazu który chcesz wyświetlić ; obraz i jego współrzędne są przekazywane do konstruktora.

Kod jest oczywiście powtarzalny dla dwóch obrazów, więc gdy ten blok zostanie umieszczony w funkcji, nie jest tak długi, jak się wydaje.

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() 

enter image description here

+0

To jest świetne, czy wiesz, jak usunąć granicę? –

+0

@JohnM Pass 'frameon = False' na' AnnotationBbox() ' – bdforbes

3

użyłbym matplotlib za to. this demo pokazuje coś podobnego, jestem pewien, że to może być dostosowany do konkretnego problemu

17

Jednym ze sposobów, aby to zrobić w R (2.11.0 lub nowszej):?

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) 
} 

zobaczyć readPNG i rasterImage dla Detale. enter image description here

1

W R, odczytany w pomocy (rasterImage):

require(grDevices) 
#set up the plot region: 
op <- par(bg = "thistle") <h> 
plot(c(100, 250), c(300, 450), type = "n", xlab="", ylab="") 
image <- as.raster(matrix(0:1, ncol=5, nrow=3)) 
rasterImage(image, 100, 300, 150, 350, interpolate=FALSE) 
rasterImage(image, 100, 400, 150, 450) 
rasterImage(image, 200, 300, 200 + xinch(.5), 300 + yinch(.3), interpolate=FALSE) 
rasterImage(image, 200, 400, 250, 450, angle=15, interpolate=FALSE) 
par(op) 

.... to dobry przykład.

3

Również w R możesz użyć funkcji my.symbols i ms.image w pakiecie TeachingDemos.

Powiązane problemy