|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider |
| 4 | +import mpl_toolkits.axes_grid1.axes_size as Size |
| 5 | + |
| 6 | +def make_heights_equal(fig, ax1, ax2, pad): |
| 7 | + # pad in inches |
| 8 | + |
| 9 | + h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1) |
| 10 | + h2, v2 = Size.AxesX(ax2), Size.AxesY(ax2) |
| 11 | + |
| 12 | + pad_v = Size.Scaled(1) |
| 13 | + pad_h = Size.Fixed(pad) |
| 14 | + |
| 15 | + my_divider = HBoxDivider(fig, 111, |
| 16 | + horizontal=[h1, pad_h, h2], |
| 17 | + vertical=[v1, pad_v, v2]) |
| 18 | + |
| 19 | + |
| 20 | + ax1.set_axes_locator(my_divider.new_locator(0)) |
| 21 | + ax2.set_axes_locator(my_divider.new_locator(2)) |
| 22 | + |
| 23 | + |
| 24 | +if __name__ == "__main__": |
| 25 | + |
| 26 | + fig1 = plt.figure() |
| 27 | + |
| 28 | + arr1 = np.arange(20).reshape((4,5)) |
| 29 | + arr2 = np.arange(20).reshape((5,4)) |
| 30 | + |
| 31 | + ax1 = plt.subplot(121) |
| 32 | + ax2 = plt.subplot(122) |
| 33 | + |
| 34 | + ax1.imshow(arr1, interpolation="nearest") |
| 35 | + ax2.imshow(arr2, interpolation="nearest") |
| 36 | + |
| 37 | + make_heights_equal(fig1, ax1, ax2, pad=0.5) |
| 38 | + |
| 39 | + for ax in [ax1, ax2]: |
| 40 | + ax.locator_params(nbins=4) |
| 41 | + |
| 42 | + # annotate |
| 43 | + ax3 = plt.axes([0.5, 0.5, 0.001, 0.001], frameon=False) |
| 44 | + ax3.xaxis.set_visible(False) |
| 45 | + ax3.yaxis.set_visible(False) |
| 46 | + ax3.annotate("Location of two axes are adjusted\n so that they have an equal height\n while maintaining their aspect ratios", (0.5, 0.5), |
| 47 | + xycoords="axes fraction", va="center", ha="center", |
| 48 | + bbox=dict(fc="w")) |
0 commit comments