Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gridliner when datalim updated by constrained layout #2254

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment indicating this needs to come before the other updates so we don't accidentally move it somewhere else later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


# 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')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem does show up if the colorbar isn't included, but is less obvious.


# 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
Loading