Skip to content

Commit e38c544

Browse files
committed
FIX: move titles up if overlap axes
TST: Add test for large subscript
1 parent 9f74ea7 commit e38c544

File tree

9 files changed

+257
-209
lines changed

9 files changed

+257
-209
lines changed

lib/matplotlib/axes/_base.py

+32-16
Original file line numberDiff line numberDiff line change
@@ -2501,34 +2501,50 @@ def _update_title_position(self, renderer):
25012501
x, y = title.get_position()
25022502
if not np.isclose(y, 1.0):
25032503
self._autotitlepos = False
2504-
_log.debug('not adjusting title pos because title was'
2504+
_log.debug('not adjusting title pos because a title was'
25052505
' already placed manually: %f', y)
25062506
return
25072507
self._autotitlepos = True
25082508

2509+
ymax = -10
25092510
for title in titles:
25102511
x, y0 = title.get_position()
2511-
y = 1.0
2512+
y = 1
2513+
# need to start again in case of window resizing
2514+
title.set_position((x, 1.0))
25122515
# need to check all our twins too...
25132516
axs = self._twinned_axes.get_siblings(self)
25142517

2518+
top = 0 # the top of all the axes twinned with this axes...
25152519
for ax in axs:
25162520
try:
25172521
if (ax.xaxis.get_label_position() == 'top'
25182522
or ax.xaxis.get_ticks_position() == 'top'):
25192523
bb = ax.xaxis.get_tightbbox(renderer)
2520-
top = bb.ymax
2521-
# we don't need to pad because the padding is already
2522-
# in __init__: titleOffsetTrans
2523-
yn = self.transAxes.inverted().transform((0., top))[1]
2524-
y = max(y, yn)
2524+
else:
2525+
bb = ax.get_window_extent(renderer)
2526+
top = max(top, bb.ymax)
25252527
except AttributeError:
2526-
pass
2527-
2528-
title.set_position((x, y))
2528+
# this happens for an empty bb
2529+
y = 1
2530+
if title.get_window_extent(renderer).ymin < top:
2531+
y = self.transAxes.inverted().transform(
2532+
(0., top))[1]
2533+
title.set_position((x, y))
2534+
# emperically, this doesn't always get the min to top,
2535+
# so we need to adjust again.
2536+
if title.get_window_extent(renderer).ymin < top:
2537+
y = self.transAxes.inverted().transform(
2538+
(0., 2 * top -
2539+
title.get_window_extent(renderer).ymin))[1]
2540+
title.set_position((x, y))
2541+
ymax = max(y, ymax)
2542+
for title in titles:
2543+
# now line up all the titles at the highest baseline.
2544+
x, y0 = title.get_position()
2545+
title.set_position((x, ymax))
25292546

25302547
# Drawing
2531-
25322548
@allow_rasterization
25332549
def draw(self, renderer=None, inframe=False):
25342550
"""Draw everything (plot lines, axes, labels)"""
@@ -4200,19 +4216,19 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
42004216
if bb_xaxis:
42014217
bb.append(bb_xaxis)
42024218

4203-
self._update_title_position(renderer)
4204-
bb.append(self.get_window_extent(renderer))
4219+
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4220+
if bb_yaxis:
4221+
bb.append(bb_yaxis)
42054222

4223+
self._update_title_position(renderer)
42064224
if self.title.get_visible():
42074225
bb.append(self.title.get_window_extent(renderer))
42084226
if self._left_title.get_visible():
42094227
bb.append(self._left_title.get_window_extent(renderer))
42104228
if self._right_title.get_visible():
42114229
bb.append(self._right_title.get_window_extent(renderer))
42124230

4213-
bb_yaxis = self.yaxis.get_tightbbox(renderer)
4214-
if bb_yaxis:
4215-
bb.append(bb_yaxis)
4231+
bb.append(self.get_window_extent(renderer))
42164232

42174233
bbox_artists = bbox_extra_artists
42184234
if bbox_artists is None:
Binary file not shown.

0 commit comments

Comments
 (0)