问题
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: 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())
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())
回答1:
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.
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])
Hope this is useful to all readers.
回答2:
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.
I think make a sense try other projection.
回答3:
For example: projection = ccrs.LambertCylindrical(central_longitude=180)
Unfortunately, "Cannot label Lambert Cylindrical grid lines. Only PlateCarree gridlines are currently supported."
来源:https://stackoverflow.com/questions/59584276/cartopy-set-extent-with-central-longitude-180