简体   繁体   中英

cartopy set extent with central_longitude=180

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_正确 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())

在此处输入图片说明

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.

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.

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM