Changing `ylim` on `cartopy` with different `central_longitude`
Question:
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
But if central_longitude=-60
It returns
My questions are:
- Why
cartopy
is behaving like this?
- How can I solve this issue?
Answers:
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 = projection=ccrs.PlateCarree() #central_longitude=0
myproj = projection=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()
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.
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
But if central_longitude=-60
It returns
My questions are:
- Why
cartopy
is behaving like this? - How can I solve this issue?
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 = projection=ccrs.PlateCarree() #central_longitude=0
myproj = projection=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()
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.