【问题标题】:Multiple picture background matplotlib多图背景matplotlib
【发布时间】:2014-10-07 11:16:51
【问题描述】:

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

我正在使用文档中的一个示例,以便绘制如下内容:

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

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

所以,尽可能清楚地说:我想在正方形 0,0 上显示一个图像,在正方形 0,1 上显示一个不同的图像......等等。

【问题讨论】:

    标签: python image matplotlib background


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2011-06-15
      • 1970-01-01
      • 2019-02-27
      • 2018-03-02
      • 2020-07-26
      • 1970-01-01
      相关资源
      最近更新 更多