Michael Dorner
Michael Dorner

Reputation: 20185

Extent in cartopy

import matplotlib.pyplot as plt
from cartopy import crs as ccrs, feature as cfeature
import cartopy.io.shapereader as shpreader


fig = plt.figure(figsize=(11, 11))
ax = plt.subplot(1, 1, 1, projection=ccrs.PlateCarree(central_longitude=0))
ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--')
extent = (-180, 0, 0, 60) # Define the extent as (min_lon, max_lon, min_lat, max_lat)
ax.set_extent(extent)
ax.add_feature(cfeature.LAND , linewidth=0.5, edgecolor='black')

returns (as I would expect)

enter image description here

As far as I understand, the extent

extent = (-180, 60, 0, 60)

should limit the scope to the 60 degree east (indicated by the red line) but it doesn't:

enter image description here

What am I doing wrong?

Upvotes: 0

Views: 376

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64463

Are you sure the first image is returned as you expect? Given that you specify the maximum latitude to be 60°, and the map ranges up to ~90°.

As the docstring for set_extent mentions, if you omit the projection of your extent, it assumes a Geodetic version.

enter image description here

So in your case, you want to be explicit (which is good practice anyway), and provide the projection of the extent yourself.

For example use the same as the projection of the map:

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

proj = ccrs.PlateCarree(central_longitude=0)
fig, ax = plt.subplots(figsize=(11, 11), subplot_kw=dict(projection=proj), facecolor="w")

ax.gridlines(draw_labels=True, lw=1, color='gray', alpha=0.5, linestyle='--')
ax.coastlines()

ax.set_extent((-180, 60, 0, 60), crs=proj)

enter image description here

If you would change the last line, and explicitly provide a Geodetic projection:

ax.set_extent((-180, 60, 0, 60), crs=ccrs.Geodetic())

You'll again end up with:

enter image description here

Upvotes: 1

Related Questions