cartopy set extent with central_longitude=180
Asked Answered
I

3

5

Cartopy 0.17.0: When I set central_longitude, I don't know how to set the extents exactly provided:

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

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45), crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

This subsets latitudes correctly: y_correct This subsets longitudes correctly, but has extra labels:

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

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45))
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

enter image description here This sets latitudes correctly:

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

projection = ccrs.PlateCarree(central_longitude=180)
ax = plt.axes(projection=projection)
ax.coastlines()
ax.set_extent((-120, 120, -45, 45), crs=projection)
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree())

enter image description here

Internationalize answered 3/1, 2020 at 19:28 Comment(0)
E
8

Using Cartopy to plot map across world dateline is not simple as you have found. It needs some tricks to get it right. The most important thing is the CRS that must be used correctly in all parts of your code.

Code:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# cartopy-0.17.0 pyshp-2.1.0

cm = 180
proj = ccrs.PlateCarree(central_longitude=cm)
fig = plt.figure(figsize=[5, 8])
ax = fig.add_subplot(1, 1, 1, projection=proj)
ax.coastlines()

# original ax.set_extent((-120, 120, -45, 45)) ?
# Need longitude extent from -60 to +60 on PlateCarree(central_longitude=180)
minlon = -60 + cm
maxlon = +60 + cm
ax.set_extent([minlon, maxlon, -45, 45], ccrs.PlateCarree())
ax.gridlines(draw_labels=True, crs=proj)
plt.show()

Output plot1, with longitude labels in PlateCarree(central_longitude=180) which is natural in itself, but not geographic norm.

enter image description here

If you want to have ordinary geographic longitude labels in the plot above, you can't simply use

ax.gridlines(draw_labels=True, crs=PlateCarree())

in the code, as you have found.

Output plot2, with ordinary geographic longitude labels

This requires specific instruction in ax.gridlines() as follows:

ax.gridlines(draw_labels=False, crs=ccrs.PlateCarree(), xlocs=[120,140,160,180,200,220,240])
ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), xlocs=[120,140,160,180,-160,-140,-120])

enter image description here

Hope this is useful to all readers.

Edraedrea answered 5/1, 2020 at 9:8 Comment(0)
L
0

It's depend with 'class PlateCarree' properties (and properties of the CylindricalProjection which is used). Please see the documentation. Longitude value 180 is a border.

If set extent [120 180 ...] or [-120 180 ...] then there are no problems.

ax.set_extent([-120, 180, -45, 45]) ax.set_extent([120, 180, -45, 45])

I think make a sense try other projection.

Leatherman answered 3/1, 2020 at 20:30 Comment(2)
I want to see the whole Pacific from -45 to 45N without splitting like that.Internationalize
I understand. So, maybe will you select other projection?Leatherman
L
0

For example: projection = ccrs.LambertCylindrical(central_longitude=180)

enter image description here

Unfortunately, "Cannot label Lambert Cylindrical grid lines. Only PlateCarree gridlines are currently supported."

Leatherman answered 3/1, 2020 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.