Чтобы получить то, что вы ожидаете, необходимо правильно использовать CRS во всех частях вашего кода. Обычно наши данные кодируются в одной CRS, обычно это geographi c (долгота, широта в градусах). В приведенном ниже коде crs0
- это CRS данных, а crs180
- это CRS проекции карты. Поскольку два CRS различны, преобразование координат должно выполняться на определенных этапах, чтобы получить правильный результат.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from shapely.geometry import Point, Polygon
plt.style.use('seaborn-dark-palette')
# define CRS's
crs0 = ccrs.PlateCarree(central_longitude=0) #for coding data
lon_0 = 180 #OK for 180, for 0, you may be surprised, but it's still OK
crs180 = ccrs.PlateCarree(central_longitude=lon_0) #for plotting map
# (coding data)
# data is in `crs0`
polys = {'SPNA': [(250, 25), (280, 25), (302, 10)],
'EP': [(178, 48), (227, 48), (227, 24)]}
# (specs of plotting, use `crs180`)
fig, axes = plt.subplots(figsize=(8,5), subplot_kw={'projection': crs180})
axes.stock_img()
for loc, poly in polys.items():
pts = []
lons, lats = [], []
for lon, lat in poly:
pt = Point(lon, lat)
# transform data (x,y) to what we need for plotting
px, py = crs180.transform_point(lon, lat, crs0)
pts.append( [px, py] )
lons.append(px)
lats.append(py)
shp = Polygon(pts)
#print (shp)
# Note the options: `transform`, and `crs`
# Our data are already in `crs180`, same as axes' CRS
# `transform` can be ignored in some places
axes.scatter(lons, lats, transform=crs180)
axes.add_geometries([shp], crs=crs180, fc="gold", ec="red")
plt.show()
обновленная карта