Skip to content

Commit

Permalink
FIX: Update the extent when calling contour
Browse files Browse the repository at this point in the history
This brings the bounds logic from contourf into contour
to properly update the extents of the plot.
  • Loading branch information
greglucas committed Jan 18, 2021
1 parent 4119741 commit ccb1c28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions lib/cartopy/tests/mpl/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit ccb1c28

Please sign in to comment.