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 2, 2023
1 parent e424784 commit cce20d6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,12 @@ def _draw_preprocess(self, renderer):
if self.get_autoscale_on() and self.ignore_existing_data_limits:
self.autoscale_view()

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):
"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 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,39 @@ 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)

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 cce20d6

Please sign in to comment.