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

Skip to content

Commit 8b27ae3

Browse files
committed
finish code and add tests
1 parent f5de111 commit 8b27ae3

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,24 +3004,21 @@ def _update_title_position(self, renderer):
30043004
(0., 2 * top - title.get_window_extent(renderer).ymin))
30053005
title.set_position((x, y))
30063006

3007+
grouped_axs = self.figure._align_title_groups.get_siblings(self)
30073008
ymax = max(title.get_position()[1] for title in titles)
30083009
for title in titles:
30093010
# now line up all the titles at the highest baseline.
30103011
x, _ = title.get_position()
30113012
title.set_position((x, ymax))
30123013

3013-
# Assign
3014-
grouped_axs = self.figure._align_title_groups.get_siblings(self)
3015-
ymax = None
3014+
# Align bboxes of grouped axes to highest in group
3015+
bb_ymax = None
30163016
ax_max = None
30173017
for ax in grouped_axs:
3018-
if ymax is None or ax.bbox.ymax > ymax:
3019-
ymax = ax.bbox.ymax
3018+
if bb_ymax is None or ax.bbox.ymax > bb_ymax:
3019+
bb_ymax = ax.bbox.ymax
30203020
ax_max = ax
3021-
# print("SELF FIRST: ", self.bbox.xmax)
3022-
# print("OTHER: ", ax_max.bbox.xmax)
30233021
self.bbox = ax_max.bbox
3024-
# print("SELF AFTER: ", self.bbox.xmax)
30253022

30263023
# Drawing
30273024
@martist.allow_rasterization

lib/matplotlib/tests/test_figure.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ def test_align_labels_stray_axes():
100100
np.testing.assert_allclose(yn[::2], yn[1::2])
101101

102102

103+
# TODO add image comparison
104+
@image_comparison(['figure_align_titles'], extensions=['png', 'svg'],
105+
tol=0 if platform.machine() == 'x86_64' else 0.01)
106+
def test_align_titles():
107+
fig, axs = plt.subplots(2, 2,
108+
subplot_kw={"xlabel": "x", "ylabel": "",
109+
"title": "Title"})
110+
axs[0][0].imshow(plt.np.zeros((5, 3)))
111+
axs[0][1].imshow(plt.np.zeros((3, 5)))
112+
axs[1][0].imshow(plt.np.zeros((2, 1)))
113+
axs[1][1].imshow(plt.np.zeros((1, 2)))
114+
115+
axs[0][1].set_title('Title2', loc="left")
116+
axs[0][1].set_title()
117+
118+
fig.align_titles()
119+
120+
121+
## TODO add image comparison
122+
@image_comparison(['figure_align_titles_some'], extensions=['png', 'svg'],
123+
tol=0 if platform.machine() == 'x86_64' else 0.01)
124+
def test_align_titles_param():
125+
fig, axs = plt.subplots(2, 2,
126+
subplot_kw={"xlabel": "x", "ylabel": "",
127+
"title": "t"})
128+
axs[0][0].imshow(plt.np.zeros((3, 5)))
129+
axs[0][1].imshow(plt.np.zeros((5, 3)))
130+
axs[1][0].imshow(plt.np.zeros((2, 1)))
131+
axs[1][1].imshow(plt.np.zeros((1, 2)))
132+
133+
fig.align_titles([axs[0][0], axs[0][1]])
134+
135+
103136
def test_figure_label():
104137
# pyplot figure creation, selection, and closing with label/number/instance
105138
plt.close('all')

0 commit comments

Comments
 (0)