Skip to content

Add test and example for VBoxDivider #23865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions examples/axes_grid1/demo_axes_hbox_divider.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 25 additions & 1 deletion lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, {})
Expand Down