Skip to content

Commit 3263cfc

Browse files
authored
Merge pull request #23865 from oscargus/dividertest
Add test and example for VBoxDivider
2 parents ee6fc0f + 8ecd60d commit 3263cfc

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed
Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,54 @@
11
"""
2-
===================
3-
`.HBoxDivider` demo
4-
===================
2+
================================
3+
HBoxDivider and VBoxDivider demo
4+
================================
55
66
Using an `.HBoxDivider` to arrange subplots.
7+
8+
Note that both axes' location are adjusted so that they have
9+
equal heights while maintaining their aspect ratios.
10+
711
"""
812

913
import numpy as np
1014
import matplotlib.pyplot as plt
11-
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
15+
from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider, VBoxDivider
1216
import mpl_toolkits.axes_grid1.axes_size as Size
1317

1418

15-
def 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))
19+
arr1 = np.arange(20).reshape((4, 5))
20+
arr2 = np.arange(20).reshape((5, 4))
21+
22+
fig, (ax1, ax2) = plt.subplots(1, 2)
23+
ax1.imshow(arr1)
24+
ax2.imshow(arr2)
2325

26+
pad = 0.5 # pad in inches
27+
divider = HBoxDivider(
28+
fig, 111,
29+
horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
30+
vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
31+
ax1.set_axes_locator(divider.new_locator(0))
32+
ax2.set_axes_locator(divider.new_locator(2))
2433

25-
if __name__ == "__main__":
34+
plt.show()
2635

27-
arr1 = np.arange(20).reshape((4, 5))
28-
arr2 = np.arange(20).reshape((5, 4))
36+
###############################################################################
37+
# Using a `.VBoxDivider` to arrange subplots.
38+
#
39+
# Note that both axes' location are adjusted so that they have
40+
# equal widths while maintaining their aspect ratios.
2941

30-
fig, (ax1, ax2) = plt.subplots(1, 2)
31-
ax1.imshow(arr1)
32-
ax2.imshow(arr2)
42+
fig, (ax1, ax2) = plt.subplots(2, 1)
43+
ax1.imshow(arr1)
44+
ax2.imshow(arr2)
3345

34-
make_heights_equal(fig, 111, ax1, ax2, pad=0.5)
46+
divider = VBoxDivider(
47+
fig, 111,
48+
horizontal=[Size.AxesX(ax1), Size.Scaled(1), Size.AxesX(ax2)],
49+
vertical=[Size.AxesY(ax1), Size.Fixed(pad), Size.AxesY(ax2)])
3550

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"))
51+
ax1.set_axes_locator(divider.new_locator(0))
52+
ax2.set_axes_locator(divider.new_locator(2))
4253

43-
plt.show()
54+
plt.show()

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from mpl_toolkits.axes_grid1.anchored_artists import (
1919
AnchoredSizeBar, AnchoredDirectionArrows)
2020
from mpl_toolkits.axes_grid1.axes_divider import (
21-
Divider, HBoxDivider, make_axes_area_auto_adjustable, SubplotDivider)
21+
Divider, HBoxDivider, make_axes_area_auto_adjustable, SubplotDivider,
22+
VBoxDivider)
2223
from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes
2324
from mpl_toolkits.axes_grid1.inset_locator import (
2425
zoomed_inset_axes, mark_inset, inset_axes, BboxConnectorPatch,
@@ -514,6 +515,29 @@ def test_hbox_divider():
514515
assert p2.width / p1.width == pytest.approx((4 / 5) ** 2)
515516

516517

518+
def test_vbox_divider():
519+
arr1 = np.arange(20).reshape((4, 5))
520+
arr2 = np.arange(20).reshape((5, 4))
521+
522+
fig, (ax1, ax2) = plt.subplots(1, 2)
523+
ax1.imshow(arr1)
524+
ax2.imshow(arr2)
525+
526+
pad = 0.5 # inches.
527+
divider = VBoxDivider(
528+
fig, 111, # Position of combined axes.
529+
horizontal=[Size.AxesX(ax1), Size.Scaled(1), Size.AxesX(ax2)],
530+
vertical=[Size.AxesY(ax1), Size.Fixed(pad), Size.AxesY(ax2)])
531+
ax1.set_axes_locator(divider.new_locator(0))
532+
ax2.set_axes_locator(divider.new_locator(2))
533+
534+
fig.canvas.draw()
535+
p1 = ax1.get_position()
536+
p2 = ax2.get_position()
537+
assert p1.width == p2.width
538+
assert p1.height / p2.height == pytest.approx((4 / 5) ** 2)
539+
540+
517541
def test_axes_class_tuple():
518542
fig = plt.figure()
519543
axes_class = (mpl_toolkits.axes_grid1.mpl_axes.Axes, {})

0 commit comments

Comments
 (0)