You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have encountered an issue with ax.set_extent(extent, ccrs.PlateCarree()) when all longitudes in the extent are negative. When I call set_extent(), the longitude range unexpectedly resets to [−180, 180] instead of keeping the specified values. This does not happen when all longitudes are positive. Additionally, in the negative longitude case, I have to manually set gl.xlocator using mticker.FixedLocator to make longitude tick labels appear, while this is not needed for positive longitudes.
Minimal Reproducible Example
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.ticker as mticker
# Define the projection
clon, clat = -145, 65 # Central longitude and latitude
proj = ccrs.LambertAzimuthalEqualArea(central_longitude=clon, central_latitude=clat)
# Define extent (all negative longitudes)
extent = [-175, -115, 57, 75]
# Create figure and axis
fig, ax = plt.subplots(figsize=(6, 4), subplot_kw={'projection': proj})
# Set extent
print("Before set_extent:", ax.get_extent(ccrs.PlateCarree()))
ax.set_extent(extent, ccrs.PlateCarree())
print("After set_extent:", ax.get_extent(ccrs.PlateCarree()))
# Add gridlines
gl = ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)
gl.top_labels = False
gl.right_labels = False
# Workaround: manually setting longitude ticks (needed only for negative longitudes)
gl.xlocator = mticker.FixedLocator([-170, -160, -150, -140, -130, -120])
plt.show()
Expected Behavior
After calling ax.set_extent(extent, ccrs.PlateCarree()), the longitude extent should remain close to [−175,−115]. Longitude tick labels should appear without needing gl.xlocator = mticker.FixedLocator(...), as is the case when using positive longitudes.
Actual Behavior
ax.set_extent(extent, ccrs.PlateCarree()) unexpectedly resets the longitude range to [−180,180].
Longitude tick labels disappear unless gl.xlocator is explicitly set. This behavior only occurs when all longitudes are negative. When all longitudes are positive, set_extent() works as expected, and tick labels are displayed without needing FixedLocator.
Additional Observations
Calling ax.get_extent(ccrs.PlateCarree()) before set_extent() gives a correct longitude range, but after calling set_extent(), the longitude range changes to [−180,180].
I think the issue here is that when we call set_extent(), we project the rectangle from PlateCarree() to the non-square LambertAzimuthalEqualArea projection and then we only keep the min/max of the projected shape extents to set the limits. This means that the bottom-left may be -175 as hoped, but the upper left is less than that because the chart got angled inwards. You can change the minimum longitude to -165 to see that it is only because it is close to the dateline that we have issues here.
There are some long standing issues describing similar behavior: #697 #1331
I have encountered an issue with
ax.set_extent(extent, ccrs.PlateCarree())
when all longitudes in the extent are negative. When I callset_extent()
, the longitude range unexpectedly resets to [−180, 180]
instead of keeping the specified values. This does not happen when all longitudes are positive. Additionally, in the negative longitude case, I have to manually setgl.xlocator
usingmticker.FixedLocator
to make longitude tick labels appear, while this is not needed for positive longitudes.Minimal Reproducible Example
Expected Behavior
After calling
ax.set_extent(extent, ccrs.PlateCarree()),
the longitude extent should remain close to[−175,−115]
. Longitude tick labels should appear without needinggl.xlocator = mticker.FixedLocator(...)
, as is the case when using positive longitudes.Actual Behavior
ax.set_extent(extent, ccrs.PlateCarree())
unexpectedly resets the longitude range to[−180,180].
Longitude tick labels disappear unless
gl.xlocator
is explicitly set. This behavior only occurs when all longitudes are negative. When all longitudes are positive,set_extent()
works as expected, and tick labels are displayed without needingFixedLocator
.Additional Observations
Calling
ax.get_extent(ccrs.PlateCarree())
beforeset_extent()
gives a correct longitude range, but after callingset_extent()
, the longitude range changes to[−180,180]
.Environment
Cartopy Version: 0.24.1
Matplotlib Version:3.10.0
Python Version:3.13.1
Operating System: Ubuntu 24.04
The text was updated successfully, but these errors were encountered: