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

Gridline for Lambert Projection #935

Closed
ShihengDuan opened this issue Jun 16, 2023 · 1 comment · Fixed by #954
Closed

Gridline for Lambert Projection #935

ShihengDuan opened this issue Jun 16, 2023 · 1 comment · Fixed by #954
Assignees
Labels

Comments

@ShihengDuan
Copy link

Hi,

I noticed the gridline is disabled for Lambert projections across 180 longitude line

if mode in ["PNA", "PDO", "NPGO", "AMO"] and projection == "Lambert":
gridline = False
.
A figure from a previous version of PMP (cannot remember the version number):
image
It seems the gridline is conflict with ax.boundary, that the longitude does not go beyond 180 degree.

max_lat = 70
min_lon = 110
max_lon = 260
max_lat = min(max_lat, 80)  
min_lat = 20
central_longitude = (min_lon + max_lon) / 2.0
central_latitude = (min_lat + max_lat) / 2.0
print(central_longitude)
projection = cartopy.crs.AlbersEqualArea(
            central_longitude=central_longitude,
            central_latitude=central_latitude,
            standard_parallels=(20, max_lat),
        )

vertices = [
            (lon, min_lat) for lon in range(int(min_lon), int(max_lon + 1), 1)
        ] + [(lon, max_lat) for lon in range(int(max_lon), int(min_lon - 1), -1)]

boundary = mpath.Path(vertices)
fig, ax = plt.subplots(nrows=1, ncols=1, subplot_kw={'projection':projection}, figsize=(8, 5))
ax.add_feature(cartopy.feature.COASTLINE)
ax.set_boundary(boundary, transform=cartopy.crs.PlateCarree())
ax.set_extent([min_lon, max_lon, min_lat, max_lat], crs=cartopy.crs.PlateCarree())
gl = ax.gridlines(draw_labels=True, alpha=0.8, linestyle="--", crs=cartopy.crs.PlateCarree())
gl.xformatter = LONGITUDE_FORMATTER 
gl.yformatter = LATITUDE_FORMATTER
gl.ylocator = mticker.FixedLocator([30, 60])
gl.xlocator = mticker.FixedLocator([120, 160, 200-360, 240-360])
gl.top_labels = False
plt.show()

Figure:
image

A possible workaround is here

max_lat = 70
min_lon = 110
max_lon = 260
max_lat = min(max_lat, 80)  
min_lat = 20
central_longitude = (min_lon + max_lon) / 2.0
central_latitude = (min_lat + max_lat) / 2.0
print(central_longitude)
projection = cartopy.crs.AlbersEqualArea(
            central_longitude=central_longitude,
            central_latitude=central_latitude,
            standard_parallels=(20, max_lat),
        )

vertices = [
            (lon-180, min_lat) for lon in range(int(min_lon), int(max_lon + 1), 1)
        ] + [(lon-180, max_lat) for lon in range(int(max_lon), int(min_lon - 1), -1)]

boundary = mpath.Path(vertices)
fig, ax = plt.subplots(nrows=1, ncols=1, subplot_kw={'projection':projection}, figsize=(8, 5))
ax.add_feature(cartopy.feature.COASTLINE)
ax.set_boundary(boundary, transform=cartopy.crs.PlateCarree(central_longitude=180))
ax.set_extent([min_lon, max_lon, min_lat, max_lat], crs=cartopy.crs.PlateCarree())
gl = ax.gridlines(draw_labels=True, alpha=0.8, linestyle="--", crs=cartopy.crs.PlateCarree())
gl.xformatter = LONGITUDE_FORMATTER 
gl.yformatter = LATITUDE_FORMATTER
gl.ylocator = mticker.FixedLocator([30, 60])
gl.xlocator = mticker.FixedLocator([120, 160, 200-360, 240-360])
gl.top_labels = False
plt.draw()
for ea in gl.ylabel_artists:
    right_label = ea.get_position()[0] > 0
    # print(ea, ea.get_position()[0], ea.get_visible())
    if right_label:
        ea.set_visible(False)
plt.show()

Figure:
image
Reference issue: SciTools/cartopy#2173 (comment)

@lee1043
Copy link
Contributor

lee1043 commented Jun 20, 2023

@ShihengDuan Thank you for posting this! I will fix it with the workaround!

@lee1043 lee1043 self-assigned this Jun 29, 2023
@lee1043 lee1043 added the bug label Jun 29, 2023
@lee1043 lee1043 linked a pull request Jun 29, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants