我正在尝试使用gridspec在matplotlib中制作一个图像网格.问题是,我似乎无法摆脱行之间的填充.
这是我对解决方案的尝试.
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid import numpy as np from os import listdir from os import chdir from PIL import Image import matplotlib.gridspec as gridspec chdir('/home/matthew/DropBox/Work/writing/'+ 'paper_preperation/jump_figs') files = listdir('/home/matthew/DropBox/Work/writing/'+ 'paper_preperation/jump_figs') images = [Image.open(f) for f in files] """ fig = plt.figure() grid = ImageGrid(fig,111,# similar to subplot(111) nrows_ncols = (2,5),# creates 2x2 grid of axes axes_pad=0.1,# pad between axes in inch. ) """ num_rows = 2 num_cols = 5 fig = plt.figure() gs = gridspec.GridSpec(num_rows,num_cols,wspace=0.0) ax = [plt.subplot(gs[i]) for i in range(num_rows*num_cols)] gs.update(hspace=0) #gs.tight_layout(fig,h_pad=0,w_pad=0) for i,im in enumerate(images): ax[i].imshow(im) ax[i].axis('off') #ax_grid[i/num_cols,i-(i/num_cols)*num_cols].imshow(im) # The AxesGrid object work as a list of axes. #ax_grid[i/num_cols,i-(i/num_cols)*num_cols].axis('off') """ all_axes = fig.get_axes() for ax in all_axes: for sp in ax.spines.values(): sp.set_visible(False) if ax.is_first_row(): ax.spines['top'].set_visible(True) if ax.is_last_row(): ax.spines['bottom'].set_visible(True) if ax.is_first_col(): ax.spines['left'].set_visible(True) if ax.is_last_col(): ax.spines['right'].set_visible(True) """ plt.show()
还有谁知道如何使每个子图更大?
解决方法
对我来说,aspect =“auto”和subplots_adjust的组合起作用.此外,我总是试图使子图变为二次.对于单个子图大小,可以调整figsize.
fig,axes = plt.subplots(nrows=max_rows,ncols=max_cols,figsize=(20,20)) for idx,image in enumerate(images): row = idx // max_cols col = idx % max_cols axes[row,col].axis("off") axes[row,col].imshow(image,cmap="gray",aspect="auto") plt.subplots_adjust(wspace=.05,hspace=.05) plt.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。