codeKiller
codeKiller

Reputation: 5739

Multiple picture background matplotlib

I need some help with a plot in python.

I am takling an example from the documentation, in order to plot something like this:

enter image description here

But my question now is if that would be possible to make a plot with this style (squares) but showing a different image on each square.

The images that I would like to show will be loaded from the computer.

So, to be as clear as possbie: I would like to show an image on the square 0,0 a different image on the square 0,1....and so on.

Upvotes: 2

Views: 438

Answers (1)

unutbu
unutbu

Reputation: 879331

One way would be to pack the image arrays into one big array and then call imshow on the big array:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.image as mimage
import scipy.misc as misc
import itertools as IT

height, width = 400, 400
nrows, ncols = 2, 4

# load some arrays into arrs
filenames = ['grace_hopper.png', 'ada.png', 'logo2.png']
arrs = list()
for filename in filenames:
    datafile = cbook.get_sample_data(filename, asfileobj=False)
    arr = misc.imresize(mimage.imread(datafile), (height, width))[..., :3]
    arrs.append(arr)
arrs = IT.islice(IT.cycle(arrs), nrows*ncols)

# make a big array
result = np.empty((nrows*height, ncols*width, 3), dtype='uint8')
for (i, j), arr in IT.izip(IT.product(range(nrows), range(ncols)), arrs):
    result[i*height:(i+1)*height, j*width:(j+1)*width] = arr

fig, ax = plt.subplots()
ax.imshow(result)
plt.show()

enter image description here

Upvotes: 2

Related Questions