问题描述
当地图的 central_longitude = 180 时,我试图在 180 经度上绘制多边形几何图形;但是会出现人工条带。
有没有办法阻止 matplotlib 出现红色圆圈中显示的条带?
import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
lats = np.linspace(-75,75,100)
lons = np.linspace(150,190,100)
gdf = gpd.GeoDataFrame(
crs=4326,geometry=gpd.points_from_xy(lons,lats)
)
gdf = gdf.to_crs({'init': 'epsg:3174'})
gdf['geometry'] = gdf['geometry'].buffer(90000)
gdf['route'] = 0
gdf = gdf.to_crs({'init': 'epsg:4326'})
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_geometries(gdf['geometry'],crs=ccrs.PlateCarree())
ax.set_extent([120,250,-75,75],crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True,xlocs=[180])
ax.coastlines()
解决方法
从 epsg:3174
重新投影到 EPSG:4326
会使穿越日期变更线的几何图形变得混乱。您可以直接使用 epsg:3174
绘制地理数据框,如以下代码所示:
import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
import numpy as np
lats = np.linspace(-75,75,100)
lons = np.linspace(150,190,100)
gdf = gpd.GeoDataFrame(
crs=4326,geometry=gpd.points_from_xy(lons,lats)
)
# gdf2 = gdf.to_crs({'init': 'epsg:3174'})
gdf2 = gdf.to_crs(epsg=3174)
gdf2['geometry'] = gdf2['geometry'].buffer(90000)
gdf2['route'] = 0
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_geometries(gdf2['geometry'],crs=ccrs.epsg(3174))
ax.set_extent([120,250,-75,75],crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True,xlocs=[180])
ax.coastlines()
plt.show()