diff --git a/examples/axes_grid1/demo_axes_hbox_divider.py b/examples/axes_grid1/demo_axes_hbox_divider.py index 7bbbeff950a6..b3bfcc508468 100644 --- a/examples/axes_grid1/demo_axes_hbox_divider.py +++ b/examples/axes_grid1/demo_axes_hbox_divider.py @@ -1,43 +1,54 @@ """ -=================== -`.HBoxDivider` demo -=================== +================================ +HBoxDivider and VBoxDivider demo +================================ Using an `.HBoxDivider` to arrange subplots. + +Note that both axes' location are adjusted so that they have +equal heights while maintaining their aspect ratios. + """ import numpy as np import matplotlib.pyplot as plt -from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider +from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider, VBoxDivider import mpl_toolkits.axes_grid1.axes_size as Size -def make_heights_equal(fig, rect, ax1, ax2, pad): - # pad in inches - divider = HBoxDivider( - fig, rect, - horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], - vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) - ax1.set_axes_locator(divider.new_locator(0)) - ax2.set_axes_locator(divider.new_locator(2)) +arr1 = np.arange(20).reshape((4, 5)) +arr2 = np.arange(20).reshape((5, 4)) + +fig, (ax1, ax2) = plt.subplots(1, 2) +ax1.imshow(arr1) +ax2.imshow(arr2) +pad = 0.5 # pad in inches +divider = HBoxDivider( + fig, 111, + horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)], + vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)]) +ax1.set_axes_locator(divider.new_locator(0)) +ax2.set_axes_locator(divider.new_locator(2)) -if __name__ == "__main__": +plt.show() - arr1 = np.arange(20).reshape((4, 5)) - arr2 = np.arange(20).reshape((5, 4)) +############################################################################### +# Using a `.VBoxDivider` to arrange subplots. +# +# Note that both axes' location are adjusted so that they have +# equal widths while maintaining their aspect ratios. - fig, (ax1, ax2) = plt.subplots(1, 2) - ax1.imshow(arr1) - ax2.imshow(arr2) +fig, (ax1, ax2) = plt.subplots(2, 1) +ax1.imshow(arr1) +ax2.imshow(arr2) - make_heights_equal(fig, 111, ax1, ax2, pad=0.5) +divider = VBoxDivider( + fig, 111, + horizontal=[Size.AxesX(ax1), Size.Scaled(1), Size.AxesX(ax2)], + vertical=[Size.AxesY(ax1), Size.Fixed(pad), Size.AxesY(ax2)]) - fig.text(.5, .5, - "Both axes' location are adjusted\n" - "so that they have equal heights\n" - "while maintaining their aspect ratios", - va="center", ha="center", - bbox=dict(boxstyle="round, pad=1", facecolor="w")) +ax1.set_axes_locator(divider.new_locator(0)) +ax2.set_axes_locator(divider.new_locator(2)) - plt.show() +plt.show() diff --git a/lib/mpl_toolkits/tests/test_axes_grid1.py b/lib/mpl_toolkits/tests/test_axes_grid1.py index ea9b55de4883..126cdd0b5ec7 100644 --- a/lib/mpl_toolkits/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/tests/test_axes_grid1.py @@ -18,7 +18,8 @@ from mpl_toolkits.axes_grid1.anchored_artists import ( AnchoredSizeBar, AnchoredDirectionArrows) from mpl_toolkits.axes_grid1.axes_divider import ( - Divider, HBoxDivider, make_axes_area_auto_adjustable, SubplotDivider) + Divider, HBoxDivider, make_axes_area_auto_adjustable, SubplotDivider, + VBoxDivider) from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes from mpl_toolkits.axes_grid1.inset_locator import ( zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch, @@ -514,6 +515,29 @@ def test_hbox_divider(): assert p2.width / p1.width == pytest.approx((4 / 5) ** 2) +def test_vbox_divider(): + arr1 = np.arange(20).reshape((4, 5)) + arr2 = np.arange(20).reshape((5, 4)) + + fig, (ax1, ax2) = plt.subplots(1, 2) + ax1.imshow(arr1) + ax2.imshow(arr2) + + pad = 0.5 # inches. + divider = VBoxDivider( + fig, 111, # Position of combined axes. + horizontal=[Size.AxesX(ax1), Size.Scaled(1), Size.AxesX(ax2)], + vertical=[Size.AxesY(ax1), Size.Fixed(pad), Size.AxesY(ax2)]) + ax1.set_axes_locator(divider.new_locator(0)) + ax2.set_axes_locator(divider.new_locator(2)) + + fig.canvas.draw() + p1 = ax1.get_position() + p2 = ax2.get_position() + assert p1.width == p2.width + assert p1.height / p2.height == pytest.approx((4 / 5) ** 2) + + def test_axes_class_tuple(): fig = plt.figure() axes_class = (mpl_toolkits.axes_grid1.mpl_axes.Axes, {})