我正试图在掩盖非洲大陆的同时在南极洲周围绘制数据。当我使用basemap
时,它可以选择使用map.fillcontinents()
轻松屏蔽大陆,basemap
考虑的大陆包括我不想掩盖的冰架。
我尝试使用我在互联网上找到的代码中的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免费下载的 land 。所以我在QGIS中编辑了这个shapefile,以删除南极洲的边界。现在的问题是我不知道如何掩盖里面 shapefile的所有内容(也找不到如何做到)。我还尝试通过设置geopandas
将前面的代码与linewidth=0
结合起来,并添加我创建的shapefile。问题是它们不完全相同:
有关如何使用shapefile或使用geopandas但不使用该行进行屏蔽的任何建议?
编辑:使用ThomasKhün之前的answer和我编辑过的shapefile会产生一个掩盖良好的南极洲/大陆,但是海岸线超出了地图的圆边:
我上传了here我使用的编辑过的shapefile,但它是没有该行的Natural Earth Data 50m land shapefile。
答案 0 :(得分:3)
这里有一个如何实现你想要的例子。我基本上遵循了Basemap example如何处理shapefiles
并添加了一些shapely magic来限制地图边界的轮廓。请注意,我首先尝试从ax.patches
中提取地图轮廓,但这种方式不起作用,因此我定义了一个半径为boundinglat
的圆,并使用Basemap坐标转换对其进行了变换功能。
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()
结果如下:
希望这有帮助。