Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title: set_extent Resets Longitude Range When All Longitudes Are Negative #2497

Open
habtie-phys opened this issue Feb 11, 2025 · 1 comment

Comments

@habtie-phys
Copy link

habtie-phys commented Feb 11, 2025

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].

Environment

Cartopy Version: 0.24.1
Matplotlib Version:3.10.0
Python Version:3.13.1
Operating System: Ubuntu 24.04

@greglucas
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants