子块高度和宽度之间的比率需要正好是图形各个部分的行数和列数之间的比率。
这可以通过
height_ratios
和
width_ratios
正在使用gridspec的参数。
import matplotlib.pyplot as plt
img = plt.imread("https://i.stack.imgur.com/9qe6z.png")
d = img.shape
rows = int(d[0]*2/3)
cols = int(d[1]*2/3)
q = {"TL": img[:rows,:cols,:],
"TR": img[:rows,cols:,:],
"BL": img[rows:,:cols,:],
"BR": img[rows:,cols:,:]}
kw = dict(height_ratios=[rows, d[0]-rows], width_ratios=[cols, d[1]-cols])
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2, gridspec_kw=kw)
ax1.imshow(q['TL'])
ax2.imshow(q['TR'])
ax3.imshow(q['BL'])
ax4.imshow(q['BR'])
for ax in (ax1, ax2, ax3, ax4):
ax.axis("off")
fig.subplots_adjust(hspace=0.1)
plt.show()