我试图在遮盖大陆的同时绘制南极洲周围的数据。当我使用basemap
时,它具有使用map.fillcontinents()
轻松掩盖大洲的选项,而basemap
考虑的大洲包括冰架,我不想掩盖。
我尝试从Internet上找到的代码中使用geopandas
。这行得通,除了海岸线在我认为是南极多边形的起点/终点处产生了一条不希望有的线:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
import geopandas as gpd
import shapely
from descartes import PolygonPatch
lats = np.arange(-90,-59,1)
lons = np.arange(0,361,1)
X, Y = np.meshgrid(lons, lats)
data = np.random.rand(len(lats),len(lons))
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
fig=plt.figure(dpi=150)
ax = fig.add_subplot(111)
m = Basemap(projection='spstere',boundinglat=-60,lon_0=180,resolution='i',round=True)
xi, yi = m(X,Y)
cf = m.contourf(xi,yi,data)
patches = []
selection = world[world.name == 'Antarctica']
for poly in selection.geometry:
if poly.geom_type == 'Polygon':
mpoly = shapely.ops.transform(m, poly)
patches.append(PolygonPatch(mpoly))
elif poly.geom_type == 'MultiPolygon':
for subpoly in poly:
mpoly = shapely.ops.transform(m, poly)
patches.append(PolygonPatch(mpoly))
else:
print(poly, 'blah')
ax.add_collection(PatchCollection(patches, match_original=True,color='w',edgecolor='k'))
当我尝试使用其他shapefile(例如可以从Natural Earth Data免费下载的landland)时,出现同一行。因此,我在QGIS中编辑了这个shapefile,以删除南极洲的边界。现在的问题是,我不知道如何屏蔽shapefile中的所有内容(也找不到如何执行此操作)。我还尝试通过设置
geopandas
将先前的代码与linewidth=0
组合在一起,并在上面添加我创建的shapefile。问题在于它们并不完全相同:关于如何使用shapefile或geopandas遮罩但没有线条的任何建议?
编辑:将ThomasKhün的先前answer与我编辑的shapefile一起使用,可以很好地遮盖南极洲/大陆,但是海岸线超出了地图的圆角:
我上传了here我使用过的经过编辑的shapefile,但是它是Natural Earth Data 50m land shapefile没有一行。
最佳答案
这是一个如何实现所需目标的示例。我基本上遵循Basemap example如何处理shapefiles
的方法,并添加了一些shapely magic将轮廓限制在地图边界。请注意,我首先尝试从ax.patches
提取地图轮廓,但是由于某种原因它不起作用,因此我定义了一个半径为boundinglat
的圆,并使用底图坐标转换功能对其进行了转换。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import shapely
from shapely.geometry import Polygon as sPolygon
boundinglat = -40
lats = np.arange(-90,boundinglat+1,1)
lons = np.arange(0,361,1)
X, Y = np.meshgrid(lons, lats)
data = np.random.rand(len(lats),len(lons))
fig, ax = plt.subplots(nrows=1, ncols=1, dpi=150)
m = Basemap(
ax = ax,
projection='spstere',boundinglat=boundinglat,lon_0=180,
resolution='i',round=True
)
xi, yi = m(X,Y)
cf = m.contourf(xi,yi,data)
#adjust the path to the shapefile here:
result = m.readshapefile(
'shapefiles/AntarcticaWGS84_contorno', 'antarctica',
zorder = 10, color = 'k', drawbounds = False)
#defining the outline of the map as shapely Polygon:
rim = [np.linspace(0,360,100),np.ones(100)*boundinglat,]
outline = sPolygon(np.asarray(m(rim[0],rim[1])).T)
#following Basemap tutorial for shapefiles
patches = []
for info, shape in zip(m.antarctica_info, m.antarctica):
#instead of a matplotlib Polygon, create first a shapely Polygon
poly = sPolygon(shape)
#check if the Polygon, or parts of it are inside the map:
if poly.intersects(outline):
#if yes, cut and insert
intersect = poly.intersection(outline)
verts = np.array(intersect.exterior.coords.xy)
patches.append(Polygon(verts.T, True))
ax.add_collection(PatchCollection(
patches, facecolor= 'w', edgecolor='k', linewidths=1., zorder=2
))
plt.show()
结果看起来像这样:
希望这可以帮助。
关于python - 用shapefile或geopandas绘制蒙版的南极洲,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50378688/