Skip to content

Commit

Permalink
Fix gridliner datalim updated by constrained layout
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Oct 6, 2023
1 parent e424784 commit caa4745
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,14 @@ def _draw_preprocess(self, renderer):
if self.get_autoscale_on() and self.ignore_existing_data_limits:
self.autoscale_view()

# apply_aspect may change the x or y data limits, so must be called
# before the patch is updated.
self.apply_aspect()

# Adjust location of background patch so that new gridlines generated
# by `draw` or `get_tightbbox` are clipped correctly.
# by `draw` or `get_tightbbox` are positioned and clipped correctly.
self.patch._adjust_location()

self.apply_aspect()

def get_tightbbox(self, renderer, *args, **kwargs):
"""
Extend the standard behaviour of
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions lib/cartopy/tests/mpl/test_gridliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
from unittest import mock

from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
Expand Down Expand Up @@ -257,6 +258,40 @@ def test_grid_labels_tight():
return fig


@pytest.mark.mpl_image_compare(
filename='gridliner_constrained_adjust_datalim.png',
tolerance=grid_label_tol)
def test_gridliner_constrained_adjust_datalim():
fig = plt.figure(figsize=(8, 4), layout="constrained")

# Make some axes that will fill the available space while maintaining
# correct aspect ratio
ax = fig.add_subplot(projection=ccrs.PlateCarree())
ax.set_aspect(aspect='equal', adjustable='datalim')

# Add some polygon to the map, with a colour bar
collection = PolyCollection(
verts=[
[[0, 0], [1, 0], [1, 1], [0, 1]],
[[1, 0], [2, 0], [2, 1], [1, 1]],
[[0, 1], [1, 1], [1, 2], [0, 2]],
[[1, 1], [2, 1], [2, 2], [1, 2]],
],
array=[1, 2, 3, 4],
)
ax.add_collection(collection)
fig.colorbar(collection, ax=ax, location='right')

# Set up the axes data limits to keep the polygon in view
ax.autoscale()

# Add some gridlines
ax.gridlines(draw_labels=["bottom", "left"], auto_update=True,
linestyle="-")

return fig


@pytest.mark.skipif(geos_version == (3, 9, 0), reason="GEOS intersection bug")
@pytest.mark.natural_earth
@pytest.mark.parametrize('proj', TEST_PROJS)
Expand Down

0 comments on commit caa4745

Please sign in to comment.