Skip to content

Commit

Permalink
FIX: title repositioning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Sep 30, 2023
1 parent 6721783 commit 1367d8b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,9 @@ def _update_title_position(self, renderer):
top = -1
for gl in gridliners:
if gl.has_labels():
# Both top and geo labels can appear at the top of the axes
for label in (gl.top_label_artists +
gl.left_label_artists +
gl.right_label_artists):
# we skip bottom labels because they are usually
# not at the top
gl.geo_label_artists):
bb = label.get_tightbbox(renderer)
top = max(top, bb.ymax)
if top < 0:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions lib/cartopy/tests/mpl/test_gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,40 @@ def test_gridliner_save_tight_bbox():
ax.set_global()
ax.gridlines(draw_labels=True, auto_update=True)
fig.savefig(io.BytesIO(), bbox_inches='tight')


@pytest.mark.mpl_image_compare(filename='gridliner_labels_title_adjust.png',
tolerance=grid_label_tol)
def test_gridliner_title_adjust():
# Test that title do not overlap labels
projs = [ccrs.Mercator(), ccrs.AlbersEqualArea(), ccrs.LambertConformal(),
ccrs.Orthographic()]

# Turn on automatic title placement (this is default in mpl rcParams but
# not in these tests).
plt.rcParams['axes.titley'] = None

fig = plt.figure(layout='constrained')
fig.get_layout_engine().set(h_pad=1/8)
for n, proj in enumerate(projs, 1):
ax = fig.add_subplot(2, 2, n, projection=proj)
ax.coastlines()
ax.gridlines(draw_labels=True)
ax.set_title(proj.__class__.__name__)

return fig


def test_gridliner_title_noadjust():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_global()
ax.set_title('foo')
ax.gridlines(draw_labels=['left', 'right'], ylocs=[-60, 0, 60])
fig.draw_without_rendering()
pos = ax.title.get_position()

# Title position shouldn't change when a label is on the top boundary.
ax.set_extent([-180, 180, -60, 60])
fig.draw_without_rendering()
assert ax.title.get_position() == pos

0 comments on commit 1367d8b

Please sign in to comment.