SciTools / cartopy

Cartopy - a cartographic python library with matplotlib support
https://scitools.org.uk/cartopy/docs/latest
BSD 3-Clause "New" or "Revised" License
1.39k stars 359 forks source link

weird output when using central_longitude != 0 #2412

Open JosephBARBIERDARNAL opened 3 weeks ago

JosephBARBIERDARNAL commented 3 weeks ago

Description

When using central_longitude=10, the output chart becomes unvalid with some horizontal lines (seems to be Canada being deformed in the case below).

Since the error is a bit too obvious, I doubt I'm first one having the issue, but I haven't found something similar on the internet/issues. Is it an expected behavior?

Code to reproduce

This is a copy-pastable example:

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

proj = ccrs.Mercator(central_longitude=10)

url = "https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/all_world.geojson"
world = gpd.read_file(url)
world = world[~world['name'].isin(["Antarctica", "Greenland"])]
world = world.to_crs(proj.proj4_init)

fig, ax = plt.subplots(figsize=(12, 8), dpi=300, subplot_kw={'projection':proj})
ax.set_axis_off()
world.boundary.plot(ax=ax)
plt.show()
Screenshot 2024-07-03 at 15 30 42

Traceback

None

Operating system

MacOS Sonoma

Cartopy version

0.23.0

lgolston commented 3 weeks ago

Yes, I think it is expected (see note in geopandas docs about objects crossing the dateline: https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.to_crs.html)

Here is a different version that should be working:

proj = ccrs.Mercator(central_longitude=10)

url = "https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/all_world.geojson"
world = gpd.read_file(url)
world = world[~world['name'].isin(["Antarctica", "Greenland"])]

fig, ax = plt.subplots(figsize=(12, 8), dpi=300, subplot_kw={'projection':proj})
ax.add_geometries(world["geometry"], crs=ccrs.PlateCarree(), facecolor='none')
plt.show()