The issue only happens with contourf, not with pcolormesh.
How can I fix the discontinuity at longitude=0 (white stripe in the plot)?
The issue only happens with contourf, not with pcolormesh.
How can I fix the discontinuity at longitude=0 (white stripe in the plot)?
A demo code and output plots for the use of .add_cyclic()
to solve the problem:
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.util as cutil
def sample_data(shape=(73, 145)):
"""Return ``lons``, ``lats`` and ``data`` of some fake data."""
nlats, nlons = shape
lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)
# lons is not complete 0-2pi here
lons = np.linspace(0, -0.1+2 * np.pi, nlons) ## Intended to make it incomplte to use cyclic
lons, lats = np.meshgrid(lons, lats)
wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)
lats = np.rad2deg(lats)
lons = np.rad2deg(lons)
data = wave + mean
return lons, lats, data
fig = plt.figure(figsize=(8, 4))
ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.NorthPolarStereo())
ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.NorthPolarStereo())
lons, lats, data = sample_data()
ax1.contourf(lons, lats, data,
transform=ccrs.PlateCarree(),
cmap='nipy_spectral')
ax1.coastlines()
ax1.set_global()
# ----------------------------------------
# add cyclic points to data and longitudes
# latitudes are unchanged
data2, lons2, lats2 = cutil.add_cyclic(data, lons, lats)
# ------------------------------------------------------
ax2.contourf(lons2, lats2, data2,
transform=ccrs.PlateCarree(),
cmap='nipy_spectral')
ax2.coastlines()
ax2.set_global()
plt.show()