Search code examples
pythonmatplotlibgiscartopy

Changing `ylim` on `cartopy` with different `central_longitude`


I want to create a map on Mercator projection from -60S to 60N but with -160W as the central longitude.

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

fig = plt.figure()
ax = fig.add_subplot(1,1,1,
        projection=ccrs.PlateCarree(central_longitude=0)
)
ax.set_extent([-180,180,-60,60])
ax.coastlines(resolution='110m')
ax.gridlines(draw_labels=True)

Returns the expected result for central_longitude=0

enter image description here

But if central_longitude=-60

It returns

enter image description here

My questions are:

  1. Why cartopy is behaving like this?
  2. How can I solve this issue?

Solution

  • You need to specify correct values in the relevant option arguments. The default values are not always valid.

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    
    noproj = ccrs.PlateCarree()  #central_longitude=0
    myproj = ccrs.PlateCarree(central_longitude=-60)
    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1,
            projection = myproj
    )
    
    # *** Dont use 180 but 179.95 to avoid mysterious error
    # longitudes -180 and +180 are sometimes interpreted as the same location
    # Also specify `crs` correctly
    ax.set_extent([-179.95, 179.95, -60, 60], crs=noproj)
    # In the more recent versions of Cartopy, you can use [-180,180,-60,60] 
    ax.coastlines(resolution='110m')
    ax.gridlines(draw_labels=True)
    
    plt.show()
    

    output1

    Edit

    Note that the labels for 60 degrees N and S are missing. To get them printed on the map you need this

    ax.set_extent([-179.95, 179.95, -60.01, 60.01], crs=noproj)
    

    as a replacement for the relevant line of code.