Search code examples
cartopy

setting cartopy set_extent for pacific ocean (160E ~ -90W or 160 ~ 270) not working


I'm trying to project the map only in the tropical pacific region which ranges from 160E to -90W (or 160 to 270) like the following example

paco_region    =  plt.axes(projection=ccrs.PlateCarree())
paco_region.coastlines()
paco_region.set_extent([160,-90,-20,20],crs=ccrs.PlateCarree())
paco_region.gridlines(crs=ccrs.PlateCarree(), draw_labels=True)

plt.show()

The problem is that cartopy is refusing to show the map from 160E (as set to left boundary) to -90W (as set to right boundary). It just shows the map from -90W to 160W (like figure below)

What I get...

How do I fix this?


Solution

  • You need some coordinate transformation and a little trick to get it done.

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    
    # define CRS's for our use case
    crs0 = ccrs.PlateCarree(central_longitude=0)     #for coding data, same as ccrs.PlateCarree()
    crs180 = ccrs.PlateCarree(central_longitude=180) #for plotting map in pacific area
    
    # For all plotting, use `crs180`
    fig, paco_region = plt.subplots(figsize=(9,5), subplot_kw={'projection': crs180})
    
    #paco_region.stock_img()  # background image check-plot
    paco_region.coastlines()
    
    paco_region.set_extent([160, 270, -20, 20], crs=crs0)
    
    # Sample plot of users' data
    # Just use regular long/lat
    lons = [175, 185, 195, 220, 250]
    lats = [15, 0, -15, 12, 18]
    # ... but specify `transform = crs0` when plot the data.
    paco_region.scatter(lons, lats, transform=crs0, color="r")
    
    # For grid-line labelling, use `crs0`
    paco_region.gridlines(crs=crs0, draw_labels=True)
    
    plt.show()
    

    pacific_plot