Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f57a3c2

Browse files
committed
Don't use ImageGrid in demo_text_rotation_mode.
The example can be written without involving axes_grid.
1 parent bc5d5b8 commit f57a3c2

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

examples/text_labels_and_annotations/demo_text_rotation_mode.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,69 @@
1717
the bounding box of the rotated text.
1818
- ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the
1919
text around the point of alignment.
20-
2120
"""
21+
2222
import matplotlib.pyplot as plt
23-
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
23+
import numpy as np
2424

2525

2626
def test_rotation_mode(fig, mode, subplot_location):
2727
ha_list = ["left", "center", "right"]
2828
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)
3240

3341
# 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")
3947

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+
)
4552

4653
# 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)
6267

6368
if mode == "default":
6469
# highlight bbox
6570
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())
6874
rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
6975
facecolor="C1", alpha=0.3, zorder=2)
7076
ax.add_patch(rect)
7177

7278

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])
7683
plt.show()
7784

7885

0 commit comments

Comments
 (0)