Skip to main content
 首页 » 编程设计

python之多图背景matplotlib

2024年10月25日10lexus

我需要一些关于 python 绘图的帮助。

我正在从文档中获取一个示例,以便绘制如下内容:

但我现在的问题是,是否可以用这种风格(正方形)制作一个图,但在每个正方形上显示不同的图像。

我想显示的图像将从计算机加载。

所以,尽可能清楚:我想在 0,0 方 block 上显示一张图片,在 0,1 方 block 上显示一张不同的图片......等等。

请您参考如下方法:

一种方法是将图像数组打包成一个大数组,然后在大数组上调用 imshow:

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