Skip to content

Commit

Permalink
Merge pull request SciTools#1655 from LiamBindle/fix/1654
Browse files Browse the repository at this point in the history
Update color limits for _wrapped_collection_fix automatically fixing SciTools#1654
  • Loading branch information
greglucas authored Sep 15, 2020
2 parents 8596051 + 9e5fa0a commit b71173b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cartopy/mpl/geocollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ def set_array(self, A):
# Now that we have prepared the collection data, call on
# through to the underlying implementation.
super(QuadMesh, self).set_array(A)

def set_clim(self, vmin=None, vmax=None):
# Update _wrapped_collection_fix color limits if it is there.
if hasattr(self, '_wrapped_collection_fix'):
self._wrapped_collection_fix.set_clim(vmin, vmax)

# Update color limits for the rest of the cells.
super().set_clim(vmin, vmax)
45 changes: 45 additions & 0 deletions lib/cartopy/tests/mpl/test_mpl_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,51 @@ def test_pcolormesh_set_array_with_mask():
ax.set_global() # make sure everything is visible


@pytest.mark.natural_earth
@ImageTesting(['pcolormesh_global_wrap3'], tolerance=tolerance)
def test_pcolormesh_set_clim_with_mask():
"""Testing that set_clim works with masked arrays properly."""
nx, ny = 33, 17
xbnds = np.linspace(-1.875, 358.125, nx, endpoint=True)
ybnds = np.linspace(91.25, -91.25, ny, endpoint=True)
xbnds, ybnds = np.meshgrid(xbnds, ybnds)

data = np.exp(np.sin(np.deg2rad(xbnds)) + np.cos(np.deg2rad(ybnds)))

# this step is not necessary, but makes the plot even harder to do (i.e.
# it really puts cartopy through its paces)
ybnds = np.append(ybnds, ybnds[:, 1:2], axis=1)
xbnds = np.append(xbnds, xbnds[:, 1:2] + 360, axis=1)
data = np.ma.concatenate([data, data[:, 0:1]], axis=1)

data = data[:-1, :-1]
data = np.ma.masked_greater(data, 2.6)

bad_initial_norm = plt.Normalize(-100, 100)

ax = plt.subplot(311, projection=ccrs.PlateCarree(-45))
c = plt.pcolormesh(xbnds, ybnds, data, transform=ccrs.PlateCarree(),
norm=bad_initial_norm)
assert c._wrapped_collection_fix is not None, \
'No pcolormesh wrapping was done when it should have been.'

ax.coastlines()
ax.set_global() # make sure everything is visible

ax = plt.subplot(312, projection=ccrs.PlateCarree(-1.87499952))
plt.pcolormesh(xbnds, ybnds, data, transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_global() # make sure everything is visible

ax = plt.subplot(313, projection=ccrs.Robinson(-2))
plt.pcolormesh(xbnds, ybnds, data, transform=ccrs.PlateCarree())
ax.coastlines()
ax.set_global() # make sure everything is visible

# Fix clims on c so that test passes
c.set_clim(data.min(), data.max())


@pytest.mark.natural_earth
@ImageTesting(['pcolormesh_limited_area_wrap'], tolerance=1.82)
def test_pcolormesh_limited_area_wrap():
Expand Down

0 comments on commit b71173b

Please sign in to comment.