1""" 2=================== 3`.HBoxDivider` demo 4=================== 5 6Using an `.HBoxDivider` to arrange subplots. 7""" 8 9import numpy as np 10import matplotlib.pyplot as plt 11from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider 12import mpl_toolkits.axes_grid1.axes_size as Size 13 14 15def make_heights_equal(fig, rect, ax1, ax2, pad): 16 # pad in inches 17 divider = HBoxDivider( 18 fig, rect, 19 horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], 20 vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) 21 ax1.set_axes_locator(divider.new_locator(0)) 22 ax2.set_axes_locator(divider.new_locator(2)) 23 24 25if __name__ == "__main__": 26 27 arr1 = np.arange(20).reshape((4, 5)) 28 arr2 = np.arange(20).reshape((5, 4)) 29 30 fig, (ax1, ax2) = plt.subplots(1, 2) 31 ax1.imshow(arr1) 32 ax2.imshow(arr2) 33 34 make_heights_equal(fig, 111, ax1, ax2, pad=0.5) 35 36 fig.text(.5, .5, 37 "Both axes' location are adjusted\n" 38 "so that they have equal heights\n" 39 "while maintaining their aspect ratios", 40 va="center", ha="center", 41 bbox=dict(boxstyle="round, pad=1", facecolor="w")) 42 43 plt.show() 44