diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 01d6add25184..f4e396b7ef7c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2501,34 +2501,50 @@ def _update_title_position(self, renderer): x, y = title.get_position() if not np.isclose(y, 1.0): self._autotitlepos = False - _log.debug('not adjusting title pos because title was' + _log.debug('not adjusting title pos because a title was' ' already placed manually: %f', y) return self._autotitlepos = True + ymax = -10 for title in titles: x, y0 = title.get_position() - y = 1.0 + y = 1 + # need to start again in case of window resizing + title.set_position((x, 1.0)) # need to check all our twins too... axs = self._twinned_axes.get_siblings(self) + top = 0 # the top of all the axes twinned with this axes... for ax in axs: try: if (ax.xaxis.get_label_position() == 'top' or ax.xaxis.get_ticks_position() == 'top'): bb = ax.xaxis.get_tightbbox(renderer) - top = bb.ymax - # we don't need to pad because the padding is already - # in __init__: titleOffsetTrans - yn = self.transAxes.inverted().transform((0., top))[1] - y = max(y, yn) + else: + bb = ax.get_window_extent(renderer) + top = max(top, bb.ymax) except AttributeError: - pass - - title.set_position((x, y)) + # this happens for an empty bb + y = 1 + if title.get_window_extent(renderer).ymin < top: + y = self.transAxes.inverted().transform( + (0., top))[1] + title.set_position((x, y)) + # emperically, this doesn't always get the min to top, + # so we need to adjust again. + if title.get_window_extent(renderer).ymin < top: + y = self.transAxes.inverted().transform( + (0., 2 * top - + title.get_window_extent(renderer).ymin))[1] + title.set_position((x, y)) + ymax = max(y, ymax) + for title in titles: + # now line up all the titles at the highest baseline. + x, y0 = title.get_position() + title.set_position((x, ymax)) # Drawing - @allow_rasterization def draw(self, renderer=None, inframe=False): """Draw everything (plot lines, axes, labels)""" @@ -4200,9 +4216,11 @@ def get_tightbbox(self, renderer, call_axes_locator=True, if bb_xaxis: bb.append(bb_xaxis) - self._update_title_position(renderer) - bb.append(self.get_window_extent(renderer)) + bb_yaxis = self.yaxis.get_tightbbox(renderer) + if bb_yaxis: + bb.append(bb_yaxis) + self._update_title_position(renderer) if self.title.get_visible(): bb.append(self.title.get_window_extent(renderer)) if self._left_title.get_visible(): @@ -4210,9 +4228,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True, if self._right_title.get_visible(): bb.append(self._right_title.get_window_extent(renderer)) - bb_yaxis = self.yaxis.get_tightbbox(renderer) - if bb_yaxis: - bb.append(bb_yaxis) + bb.append(self.get_window_extent(renderer)) bbox_artists = bbox_extra_artists if bbox_artists is None: diff --git a/lib/matplotlib/tests/baseline_images/test_text/large_subscript_title.png b/lib/matplotlib/tests/baseline_images/test_text/large_subscript_title.png new file mode 100644 index 000000000000..978f1291455b Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_text/large_subscript_title.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.pdf b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.pdf index 1a5a33aa6f40..c414e59fe1b8 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.pdf and b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.png b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.png index ccf27999a0bb..00c8afb79604 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.png and b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.svg b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.svg index c9d8594b05fd..4bc99a39910f 100644 --- a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.svg +++ b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout1.svg @@ -1,7 +1,7 @@ - + - - - + + + - + - + @@ -146,24 +148,24 @@ Q 35.15625 39.890625 26.703125 39.890625 Q 22.75 39.890625 18.8125 39.015625 Q 14.890625 38.140625 10.796875 36.28125 z -" id="DejaVuSans-35"/> +" id="DejaVuSans-53"/> - - - + + + - + - + @@ -181,12 +183,12 @@ L 54.390625 8.296875 L 54.390625 0 L 12.40625 0 z -" id="DejaVuSans-31"/> +" id="DejaVuSans-49"/> - - - + + + @@ -206,19 +208,19 @@ L 15.28125 54.6875 L 29.78125 35.203125 L 44.28125 54.6875 z -" id="DejaVuSans-78"/> +" id="DejaVuSans-120"/> +" id="DejaVuSans-45"/> +" id="DejaVuSans-108"/> +z +" id="DejaVuSans-97"/> +" id="DejaVuSans-98"/> +" id="DejaVuSans-101"/> - - - - - - - + + + + + + + @@ -315,60 +320,60 @@ z +" id="m556f96d829" style="stroke:#000000;stroke-width:0.5;"/> - + +" id="m27e32ca04a" style="stroke:#000000;stroke-width:0.5;"/> - + - - - + + + - + - + - - - - + + + + - + - + @@ -396,12 +401,13 @@ Q 53.21875 48.921875 51.53125 44.890625 Q 49.859375 40.875 45.40625 35.40625 Q 44.1875 33.984375 37.640625 27.21875 Q 31.109375 20.453125 19.1875 8.296875 -" id="DejaVuSans-32"/> +z +" id="DejaVuSans-50"/> - - - - + + + + @@ -423,16 +429,16 @@ L 29.59375 11.921875 L 46.6875 54.6875 L 56.203125 54.6875 z -" id="DejaVuSans-79"/> +" id="DejaVuSans-121"/> - - - - - - - - + + + + + + + + @@ -448,7 +454,7 @@ L 25.59375 0 L 25.59375 64.59375 L -0.296875 64.59375 z -" id="DejaVuSans-54"/> +" id="DejaVuSans-84"/> +" id="DejaVuSans-105"/> +" id="DejaVuSans-116"/> - - - - - - + + + + + + - - + + diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.pdf b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.pdf index 211bba218f03..b352ed0ebadd 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.pdf and b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.png b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.png index f7ac3618772b..fa02aad51ce1 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.png and b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.svg b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.svg index 6dfb4cff8aca..c0aa6d0755a3 100644 --- a/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.svg +++ b/lib/matplotlib/tests/baseline_images/test_tightlayout/tight_layout7.svg @@ -1,7 +1,7 @@ - + - - - + + + - + - + @@ -146,24 +148,24 @@ Q 35.15625 39.890625 26.703125 39.890625 Q 22.75 39.890625 18.8125 39.015625 Q 14.890625 38.140625 10.796875 36.28125 z -" id="DejaVuSans-35"/> +" id="DejaVuSans-53"/> - - - + + + - + - + @@ -181,12 +183,12 @@ L 54.390625 8.296875 L 54.390625 0 L 12.40625 0 z -" id="DejaVuSans-31"/> +" id="DejaVuSans-49"/> - - - + + + @@ -206,19 +208,19 @@ L 15.28125 54.6875 L 29.78125 35.203125 L 44.28125 54.6875 z -" id="DejaVuSans-78"/> +" id="DejaVuSans-120"/> +" id="DejaVuSans-45"/> +" id="DejaVuSans-108"/> +z +" id="DejaVuSans-97"/> +" id="DejaVuSans-98"/> +" id="DejaVuSans-101"/> - - - - - - - + + + + + + + @@ -315,60 +320,60 @@ z +" id="m556f96d829" style="stroke:#000000;stroke-width:0.5;"/> - + +" id="m27e32ca04a" style="stroke:#000000;stroke-width:0.5;"/> - + - - - + + + - + - + - - - - + + + + - + - + @@ -396,12 +401,13 @@ Q 53.21875 48.921875 51.53125 44.890625 Q 49.859375 40.875 45.40625 35.40625 Q 44.1875 33.984375 37.640625 27.21875 Q 31.109375 20.453125 19.1875 8.296875 -" id="DejaVuSans-32"/> +z +" id="DejaVuSans-50"/> - - - - + + + + @@ -423,16 +429,16 @@ L 29.59375 11.921875 L 46.6875 54.6875 L 56.203125 54.6875 z -" id="DejaVuSans-79"/> +" id="DejaVuSans-121"/> - - - - - - - - + + + + + + + + @@ -446,7 +452,7 @@ L 55.171875 8.296875 L 55.171875 0 L 9.8125 0 z -" id="DejaVuSans-4c"/> +" id="DejaVuSans-76"/> +" id="DejaVuSans-102"/> - +" id="DejaVuSans-116"/> + +" id="DejaVuSans-84"/> +" id="DejaVuSans-105"/> - - - - - - - - - - - + + + + + + + + + + + @@ -543,6 +549,7 @@ Q 44.578125 72.90625 50.734375 67.671875 Q 56.890625 62.453125 56.890625 51.90625 Q 56.890625 45.015625 53.6875 40.46875 Q 50.484375 35.9375 44.390625 34.1875 +z M 19.671875 64.796875 L 19.671875 38.921875 L 32.078125 38.921875 @@ -551,7 +558,7 @@ Q 46.484375 45.515625 46.484375 51.90625 Q 46.484375 58.296875 42.84375 61.546875 Q 39.203125 64.796875 32.078125 64.796875 z -" id="DejaVuSans-52"/> +" id="DejaVuSans-82"/> +" id="DejaVuSans-103"/> +z +" id="DejaVuSans-104"/> - - - - - - - - - - - - + + + + + + + + + + + + - - + + diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index a001aadd39a3..821d0e2f4c17 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -510,3 +510,20 @@ def test_annotation_update(): assert not np.allclose(extent1.get_points(), extent2.get_points(), rtol=1e-6) + + +@image_comparison(baseline_images=['large_subscript_title'], + extensions=['png'], style='mpl20') +def test_large_subscript_title(): + fig, axs = plt.subplots(1, 2, figsize=(9, 2.5), constrained_layout=True) + ax = axs[0] + ax.set_title('$\sum_{i} x_i$') + ax.set_title('New way', loc='left') + ax.set_xticklabels('') + + ax = axs[1] + tt = ax.set_title('$\sum_{i} x_i$') + x, y = tt.get_position() + tt.set_position((x, 1.01)) + ax.set_title('Old Way', loc='left') + ax.set_xticklabels('')