|
17 | 17 | the bounding box of the rotated text.
|
18 | 18 | - ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the
|
19 | 19 | text around the point of alignment.
|
20 |
| -
|
21 | 20 | """
|
| 21 | + |
22 | 22 | import matplotlib.pyplot as plt
|
23 |
| -from mpl_toolkits.axes_grid1.axes_grid import ImageGrid |
| 23 | +import numpy as np |
24 | 24 |
|
25 | 25 |
|
26 | 26 | def test_rotation_mode(fig, mode, subplot_location):
|
27 | 27 | ha_list = ["left", "center", "right"]
|
28 | 28 | va_list = ["top", "center", "baseline", "bottom"]
|
29 |
| - grid = ImageGrid(fig, subplot_location, |
30 |
| - nrows_ncols=(len(va_list), len(ha_list)), |
31 |
| - share_all=True, aspect=True, cbar_mode=None) |
| 29 | + axs = np.empty((len(va_list), len(ha_list)), object) |
| 30 | + gs = subplot_location.subgridspec(*axs.shape, hspace=0, wspace=0) |
| 31 | + axs[0, 0] = fig.add_subplot(gs[0, 0]) |
| 32 | + for i in range(len(va_list)): |
| 33 | + for j in range(len(ha_list)): |
| 34 | + if (i, j) == (0, 0): |
| 35 | + continue # Already set. |
| 36 | + axs[i, j] = fig.add_subplot( |
| 37 | + gs[i, j], sharex=axs[0, 0], sharey=axs[0, 0]) |
| 38 | + for ax in axs.flat: |
| 39 | + ax.set(aspect=1) |
32 | 40 |
|
33 | 41 | # labels and title
|
34 |
| - for ha, ax in zip(ha_list, grid.axes_row[-1]): |
35 |
| - ax.axis["bottom"].label.set_text(ha) |
36 |
| - for va, ax in zip(va_list, grid.axes_column[0]): |
37 |
| - ax.axis["left"].label.set_text(va) |
38 |
| - grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'", size="large") |
| 42 | + for ha, ax in zip(ha_list, axs[-1, :]): |
| 43 | + ax.set_xlabel(ha) |
| 44 | + for va, ax in zip(va_list, axs[:, 0]): |
| 45 | + ax.set_ylabel(va) |
| 46 | + axs[0, 1].set_title(f"rotation_mode='{mode}'", size="large") |
39 | 47 |
|
40 |
| - if mode == "default": |
41 |
| - kw = dict() |
42 |
| - else: |
43 |
| - kw = dict( |
44 |
| - bbox=dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3)) |
| 48 | + kw = ( |
| 49 | + {} if mode == "default" else |
| 50 | + {"bbox": dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3)} |
| 51 | + ) |
45 | 52 |
|
46 | 53 | # use a different text alignment in each axes
|
47 |
| - texts = [] |
48 |
| - for (va, ha), ax in zip([(x, y) for x in va_list for y in ha_list], grid): |
49 |
| - # prepare axes layout |
50 |
| - for axis in ax.axis.values(): |
51 |
| - axis.toggle(ticks=False, ticklabels=False) |
52 |
| - ax.axvline(0.5, color="skyblue", zorder=0) |
53 |
| - ax.axhline(0.5, color="skyblue", zorder=0) |
54 |
| - ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1) |
55 |
| - |
56 |
| - # add text with rotation and alignment settings |
57 |
| - tx = ax.text(0.5, 0.5, "Tpg", |
58 |
| - size="x-large", rotation=40, |
59 |
| - horizontalalignment=ha, verticalalignment=va, |
60 |
| - rotation_mode=mode, **kw) |
61 |
| - texts.append(tx) |
| 54 | + for i, va in enumerate(va_list): |
| 55 | + for j, ha in enumerate(ha_list): |
| 56 | + ax = axs[i, j] |
| 57 | + # prepare axes layout |
| 58 | + ax.set(xticks=[], yticks=[]) |
| 59 | + ax.axvline(0.5, color="skyblue", zorder=0) |
| 60 | + ax.axhline(0.5, color="skyblue", zorder=0) |
| 61 | + ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1) |
| 62 | + # add text with rotation and alignment settings |
| 63 | + tx = ax.text(0.5, 0.5, "Tpg", |
| 64 | + size="x-large", rotation=40, |
| 65 | + horizontalalignment=ha, verticalalignment=va, |
| 66 | + rotation_mode=mode, **kw) |
62 | 67 |
|
63 | 68 | if mode == "default":
|
64 | 69 | # highlight bbox
|
65 | 70 | fig.canvas.draw()
|
66 |
| - for ax, tx in zip(grid, texts): |
67 |
| - bb = tx.get_window_extent().transformed(ax.transData.inverted()) |
| 71 | + for ax in axs.flat: |
| 72 | + text, = ax.texts |
| 73 | + bb = text.get_window_extent().transformed(ax.transData.inverted()) |
68 | 74 | rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
|
69 | 75 | facecolor="C1", alpha=0.3, zorder=2)
|
70 | 76 | ax.add_patch(rect)
|
71 | 77 |
|
72 | 78 |
|
73 |
| -fig = plt.figure(figsize=(8, 6)) |
74 |
| -test_rotation_mode(fig, "default", 121) |
75 |
| -test_rotation_mode(fig, "anchor", 122) |
| 79 | +fig = plt.figure(figsize=(8, 5)) |
| 80 | +gs = fig.add_gridspec(1, 2) |
| 81 | +test_rotation_mode(fig, "default", gs[0]) |
| 82 | +test_rotation_mode(fig, "anchor", gs[1]) |
76 | 83 | plt.show()
|
77 | 84 |
|
78 | 85 |
|
|
0 commit comments