|
1 | 1 | """
|
2 |
| -=================== |
3 |
| -`.HBoxDivider` demo |
4 |
| -=================== |
| 2 | +================================ |
| 3 | +HBoxDivider and VBoxDivider demo |
| 4 | +================================ |
5 | 5 |
|
6 | 6 | 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 | +
|
7 | 11 | """
|
8 | 12 |
|
9 | 13 | import numpy as np
|
10 | 14 | 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 |
12 | 16 | import mpl_toolkits.axes_grid1.axes_size as Size
|
13 | 17 |
|
14 | 18 |
|
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) |
23 | 25 |
|
| 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)) |
24 | 33 |
|
25 |
| -if __name__ == "__main__": |
| 34 | +plt.show() |
26 | 35 |
|
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. |
29 | 41 |
|
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) |
33 | 45 |
|
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)]) |
35 | 50 |
|
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)) |
42 | 53 |
|
43 |
| - plt.show() |
| 54 | +plt.show() |
0 commit comments