From ccb1c28c5b05874849cde5850015c0f92b312e7d Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sat, 16 Jan 2021 09:33:36 -0700 Subject: [PATCH] FIX: Update the extent when calling contour This brings the bounds logic from contourf into contour to properly update the extents of the plot. --- lib/cartopy/mpl/geoaxes.py | 8 ++++++++ lib/cartopy/tests/mpl/test_contour.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/cartopy/mpl/geoaxes.py b/lib/cartopy/mpl/geoaxes.py index 101021dc1..ba557a9eb 100644 --- a/lib/cartopy/mpl/geoaxes.py +++ b/lib/cartopy/mpl/geoaxes.py @@ -1547,6 +1547,14 @@ def contour(self, *args, **kwargs): """ result = matplotlib.axes.Axes.contour(self, *args, **kwargs) + # We need to compute the dataLim correctly for contours. + bboxes = [col.get_datalim(self.transData) + for col in result.collections + if col.get_paths()] + if bboxes: + extent = mtransforms.Bbox.union(bboxes) + self.dataLim.update_from_data_xy(extent.get_points()) + self.autoscale_view() # Re-cast the contour as a GeoContourSet. diff --git a/lib/cartopy/tests/mpl/test_contour.py b/lib/cartopy/tests/mpl/test_contour.py index ada0bf161..fbe3fd943 100644 --- a/lib/cartopy/tests/mpl/test_contour.py +++ b/lib/cartopy/tests/mpl/test_contour.py @@ -61,3 +61,15 @@ def test_contour_linear_ring(): ax.contourf(lons, lats, vals, np.arange(9), transform=ccrs.PlateCarree()) plt.draw() + + +def test_contour_update_bounds(): + """Test that contour updates the extent""" + xs, ys = np.meshgrid(np.linspace(0, 360), np.linspace(-80, 80)) + zs = ys**2 + ax = plt.axes(projection=ccrs.Orthographic()) + ax.contour(xs, ys, zs, transform=ccrs.PlateCarree()) + # Force a draw, which is a smoke test to make sure contouring + # doesn't raise with an Orthographic projection + # GH issue 1673 + plt.draw()