Issue with Cartopy automatically resetting extent when adding data

I am trying to map some data with Cartopy but I am experiencing issues with the map extent. When plotting the map on itself Cartopy displays the entire globe (that’s great, that’s what I want):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
# Initialise plot
cm = 1/2.54
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm),
subplot_kw={'projection': ccrs.Mollweide()})
# Add land
ax.add_feature(cfeature.NaturalEarthFeature(category='physical',
name='land',
scale='50m'),
facecolor='#ccc',
edgecolor='black',
linewidth=.25,
zorder=1)
# Export
fpath = "figures/test_map1.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)
</code>
<code>import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature # Initialise plot cm = 1/2.54 fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm), subplot_kw={'projection': ccrs.Mollweide()}) # Add land ax.add_feature(cfeature.NaturalEarthFeature(category='physical', name='land', scale='50m'), facecolor='#ccc', edgecolor='black', linewidth=.25, zorder=1) # Export fpath = "figures/test_map1.png" fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300) </code>
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature


# Initialise plot
cm = 1/2.54
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm),
                       subplot_kw={'projection': ccrs.Mollweide()})

# Add land
ax.add_feature(cfeature.NaturalEarthFeature(category='physical',
                                            name='land',
                                            scale='50m'),
               facecolor='#ccc',
               edgecolor='black',
               linewidth=.25,
               zorder=1)

# Export
fpath = "figures/test_map1.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)

enter image description here

But when adding some data (e.g., as scatter), Cartopy automatically resets the scale to show only the geographic area occupied by the data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Create and plot dummy data
xv = [*range(-50, 0)]
yv = [*range(-20, 30)]
ax.scatter(xv,
yv,
edgecolor='red',
s=20,
marker='.',
linewidth=2,
transform=ccrs.PlateCarree(),
zorder=0)
fpath = "figures/test_map2.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)
</code>
<code># Create and plot dummy data xv = [*range(-50, 0)] yv = [*range(-20, 30)] ax.scatter(xv, yv, edgecolor='red', s=20, marker='.', linewidth=2, transform=ccrs.PlateCarree(), zorder=0) fpath = "figures/test_map2.png" fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300) </code>
# Create and plot dummy data
xv = [*range(-50, 0)]
yv = [*range(-20, 30)]
ax.scatter(xv,
           yv,
           edgecolor='red',
           s=20,
           marker='.',
           linewidth=2,
           transform=ccrs.PlateCarree(),
           zorder=0)
fpath = "figures/test_map2.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)

enter image description here

That would be ok if resetting it back manually wouldn’t be tricky: set_extent() does not work if longitude limits are set to -180, 180 (the map disappears, I understand that due to limit issues), and setting -179.9999, 179.9999 leaves small cuts at the sides:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Try to reset extent
ax.set_extent([-179.9999, 179.9999, -90, 90], crs=ccrs.PlateCarree())
fpath = "figures/test_map3.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)
</code>
<code># Try to reset extent ax.set_extent([-179.9999, 179.9999, -90, 90], crs=ccrs.PlateCarree()) fpath = "figures/test_map3.png" fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300) </code>
# Try to reset extent
ax.set_extent([-179.9999, 179.9999, -90, 90], crs=ccrs.PlateCarree())
fpath = "figures/test_map3.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)

enter image description here

enter image description here

My problem gets worse with other projections, e.g., NearsidePerspective, as it’s not straightforward to me how to know the maximum extent that they accept [The Cartopy documentation on projections does not address this matter as far as I’m aware]. For instance, setting extent to [-90, 90, -90, 90] with the NearsidePerspective projection
returns the following error:

ValueError: Axis limits cannot be NaN or Inf

And setting more restricted extents produces unsatisfactory results (i.e., again, border cuts):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm),
subplot_kw={'projection': ccrs.NearsidePerspective(
central_longitude=0,
central_latitude=0
)})
ax.add_feature(cfeature.NaturalEarthFeature(category='physical',
name='land',
scale='50m'),
facecolor='#ccc',
edgecolor='black',
linewidth=.25,
zorder=1)
ax.scatter(xv,
yv,
edgecolor='red',
s=20,
marker='.',
linewidth=2,
transform=ccrs.PlateCarree(),
zorder=0)
ax.set_extent([-70, 70, -60, 60], crs=ccrs.PlateCarree())
fpath = "figures/test_map4.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)
</code>
<code>fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm), subplot_kw={'projection': ccrs.NearsidePerspective( central_longitude=0, central_latitude=0 )}) ax.add_feature(cfeature.NaturalEarthFeature(category='physical', name='land', scale='50m'), facecolor='#ccc', edgecolor='black', linewidth=.25, zorder=1) ax.scatter(xv, yv, edgecolor='red', s=20, marker='.', linewidth=2, transform=ccrs.PlateCarree(), zorder=0) ax.set_extent([-70, 70, -60, 60], crs=ccrs.PlateCarree()) fpath = "figures/test_map4.png" fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300) </code>
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15*cm, 8*cm),
                       subplot_kw={'projection': ccrs.NearsidePerspective(
                           central_longitude=0,
                           central_latitude=0
                           )})
ax.add_feature(cfeature.NaturalEarthFeature(category='physical',
                                            name='land',
                                            scale='50m'),
               facecolor='#ccc',
               edgecolor='black',
               linewidth=.25,
               zorder=1)
ax.scatter(xv,
           yv,
           edgecolor='red',
           s=20,
           marker='.',
           linewidth=2,
           transform=ccrs.PlateCarree(),
           zorder=0)
ax.set_extent([-70, 70, -60, 60], crs=ccrs.PlateCarree())
fpath = "figures/test_map4.png"
fig.savefig(fpath, format='png', bbox_inches='tight', dpi=300)

enter image description here

I have also tried changing the figure size and proportions, but it does not seem to work. So, my questions would be:

  1. Is there a way to avoid the automatic reset of limits in the first place?
  2. If not, what would be the appropriate way to deal with it given the aforementioned issues?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật