diff --git a/lib/cartopy/mpl/geoaxes.py b/lib/cartopy/mpl/geoaxes.py index e06d830a8..f6a3be59b 100644 --- a/lib/cartopy/mpl/geoaxes.py +++ b/lib/cartopy/mpl/geoaxes.py @@ -538,8 +538,10 @@ def _update_title_position(self, renderer): # Get the max ymax of all top labels top = -1 for gl in gridliners: - if gl.has_labels(): - # Both top and geo labels can appear at the top of the axes + # Both top and geo labels can appear at the top of the axes + if gl.top_labels or gl.geo_labels: + # Make sure Gridliner is populated and up-to-date + gl._draw_gridliner(renderer=renderer) for label in (gl.top_label_artists + gl.geo_label_artists): bb = label.get_tightbbox(renderer) diff --git a/lib/cartopy/tests/mpl/baseline_images/mpl/test_gridliner/gridliner_labels_title_adjust.png b/lib/cartopy/tests/mpl/baseline_images/mpl/test_gridliner/gridliner_labels_title_adjust.png index 9ed9c4ffe..648f55f6a 100644 Binary files a/lib/cartopy/tests/mpl/baseline_images/mpl/test_gridliner/gridliner_labels_title_adjust.png and b/lib/cartopy/tests/mpl/baseline_images/mpl/test_gridliner/gridliner_labels_title_adjust.png differ diff --git a/lib/cartopy/tests/mpl/test_gridliner.py b/lib/cartopy/tests/mpl/test_gridliner.py index 8934e3d83..0b59aa56a 100644 --- a/lib/cartopy/tests/mpl/test_gridliner.py +++ b/lib/cartopy/tests/mpl/test_gridliner.py @@ -561,6 +561,20 @@ def test_gridliner_title_noadjust(): assert ax.title.get_position() == pos +def test_gridliner_title_adjust_no_layout_engine(): + fig = plt.figure() + ax = fig.add_subplot(projection=ccrs.PlateCarree()) + gl = ax.gridlines(draw_labels=True) + title = ax.set_title("MY TITLE") + + # After first draw, title should be above top labels. + fig.draw_without_rendering() + max_label_y = max([bb.get_tightbbox().ymax for bb in gl.top_label_artists]) + min_title_y = title.get_tightbbox().ymin + + assert min_title_y > max_label_y + + def test_gridliner_labels_zoom(): fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())