Search code examples
pythonmatplotlibcartopy

White line in contour plot in cartopy on center_longitude


I'm plotting some filled contours with Cartopy and Matplotlib. The data is on a latitude/longitude grid, and when plotting on a cartopy projection, a white line runs down the middle of the figure, or wherever I set "central_longitude" into in ccrs.PlateCarree()

Here is a quick setup that shows what I'm talking about. Using the code:

    import numpy as np
    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature

    lon = np.arange(0, 360, 1)
    lat = np.arange(-90, 90, 1)

    data = np.zeros((180, 360))

    fig = plt.figure()
    ax = plt.subplot(projection=ccrs.PlateCarree())
    ax.contourf(lon, lat, data)
    ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
    plt.show()

Which produces the image: enter image description here

Is there a way to remove this white line?


Solution

  • You should use cartopy.util.add_cyclic_point so that contourf sees the data as continuous in the x-direction and the white line will disappear:

    import numpy as np
    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    from cartopy.util import add_cyclic_point
    
    lon = np.arange(0, 360, 1)
    lat = np.arange(-90, 90, 1)
    
    data = np.zeros((180, 360))
    
    data, lon = add_cyclic_point(data, coord=lon)
    
    fig = plt.figure()
    ax = plt.subplot(projection=ccrs.PlateCarree())
    ax.contourf(lon, lat, data)
    ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
    plt.show()