我需要一些有关 python 绘图的帮助。
我正在使用文档中的一个示例,以便绘制如下所示的内容:
但我现在的问题是,是否可以使用这种样式(方 block )绘制绘图,但在每个方 block 上显示不同的图像。
将从计算机加载我想要显示的图像。
所以,尽可能清楚地说:我想在正方形 0,0 上显示一个图像,在正方形 0,1 上显示一个不同的图像......等等。
最佳答案
一种方法是将图像数组打包到一个大数组中,然后在该大数组上调用 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()
关于python - 多图片背景matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26234751/