Skip to content

Don't use ImageGrid in demo_text_rotation_mode. #15281

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
Apr 23, 2021
Merged
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
77 changes: 42 additions & 35 deletions examples/text_labels_and_annotations/demo_text_rotation_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,69 @@
the bounding box of the rotated text.
- ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the
text around the point of alignment.

"""

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
import numpy as np


def test_rotation_mode(fig, mode, subplot_location):
ha_list = ["left", "center", "right"]
va_list = ["top", "center", "baseline", "bottom"]
grid = ImageGrid(fig, subplot_location,
nrows_ncols=(len(va_list), len(ha_list)),
share_all=True, aspect=True, cbar_mode=None)
axs = np.empty((len(va_list), len(ha_list)), object)
gs = subplot_location.subgridspec(*axs.shape, hspace=0, wspace=0)
axs[0, 0] = fig.add_subplot(gs[0, 0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part would be slightly easier with something like #14421 available.

for i in range(len(va_list)):
for j in range(len(ha_list)):
if (i, j) == (0, 0):
continue # Already set.
axs[i, j] = fig.add_subplot(
gs[i, j], sharex=axs[0, 0], sharey=axs[0, 0])
for ax in axs.flat:
ax.set(aspect=1)

# labels and title
for ha, ax in zip(ha_list, grid.axes_row[-1]):
ax.axis["bottom"].label.set_text(ha)
for va, ax in zip(va_list, grid.axes_column[0]):
ax.axis["left"].label.set_text(va)
grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'", size="large")
for ha, ax in zip(ha_list, axs[-1, :]):
ax.set_xlabel(ha)
for va, ax in zip(va_list, axs[:, 0]):
ax.set_ylabel(va)
axs[0, 1].set_title(f"rotation_mode='{mode}'", size="large")

if mode == "default":
kw = dict()
else:
kw = dict(
bbox=dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3))
kw = (
{} if mode == "default" else
{"bbox": dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3)}
)

# use a different text alignment in each axes
texts = []
for (va, ha), ax in zip([(x, y) for x in va_list for y in ha_list], grid):
# prepare axes layout
for axis in ax.axis.values():
axis.toggle(ticks=False, ticklabels=False)
ax.axvline(0.5, color="skyblue", zorder=0)
ax.axhline(0.5, color="skyblue", zorder=0)
ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1)

# add text with rotation and alignment settings
tx = ax.text(0.5, 0.5, "Tpg",
size="x-large", rotation=40,
horizontalalignment=ha, verticalalignment=va,
rotation_mode=mode, **kw)
texts.append(tx)
for i, va in enumerate(va_list):
for j, ha in enumerate(ha_list):
ax = axs[i, j]
# prepare axes layout
ax.set(xticks=[], yticks=[])
ax.axvline(0.5, color="skyblue", zorder=0)
ax.axhline(0.5, color="skyblue", zorder=0)
ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1)
# add text with rotation and alignment settings
tx = ax.text(0.5, 0.5, "Tpg",
size="x-large", rotation=40,
horizontalalignment=ha, verticalalignment=va,
rotation_mode=mode, **kw)

if mode == "default":
# highlight bbox
fig.canvas.draw()
for ax, tx in zip(grid, texts):
bb = tx.get_window_extent().transformed(ax.transData.inverted())
for ax in axs.flat:
text, = ax.texts
bb = text.get_window_extent().transformed(ax.transData.inverted())
rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
facecolor="C1", alpha=0.3, zorder=2)
ax.add_patch(rect)


fig = plt.figure(figsize=(8, 6))
test_rotation_mode(fig, "default", 121)
test_rotation_mode(fig, "anchor", 122)
fig = plt.figure(figsize=(8, 5))
gs = fig.add_gridspec(1, 2)
test_rotation_mode(fig, "default", gs[0])
test_rotation_mode(fig, "anchor", gs[1])
plt.show()


Expand Down