Matplotlib 使用Cartopy合并国家/地区

Matplotlib 使用Cartopy合并国家/地区,matplotlib,geopandas,cartopy,Matplotlib,Geopandas,Cartopy,我使用以下代码将瑞典、挪威和芬兰作为一个地区绘制地图。然而,我正在与之斗争。下面是这个示例,Matplotlib Cartopy Color One Country中的Python映射 from shapely.geometry import Polygon from cartopy.io import shapereader import cartopy.io.img_tiles as cimgt import cartopy.crs as ccrs import geopandas impo

我使用以下代码将瑞典、挪威和芬兰作为一个地区绘制地图。然而,我正在与之斗争。下面是这个示例,Matplotlib Cartopy Color One Country中的Python映射

from shapely.geometry import Polygon
from cartopy.io import shapereader
import cartopy.io.img_tiles as cimgt
import cartopy.crs as ccrs
import geopandas
import matplotlib.pyplot as plt


def rect_from_bound(xmin, xmax, ymin, ymax):
    """Returns list of (x,y)'s for a rectangle"""
    xs = [xmax, xmin, xmin, xmax, xmax]
    ys = [ymax, ymax, ymin, ymin, ymax]
    return [(x, y) for x, y in zip(xs, ys)]

# request data for use by geopandas
resolution          = '10m'
category            = 'cultural'
name                = 'admin_0_countries'
countries           = ['Norway', 'Sweden', 'Finland']
shpfilename         = shapereader.natural_earth(resolution, category, name)
df                  = geopandas.read_file(shpfilename)
extent              = [2, 32, 55, 72]
# get geometry of a country
for country in (countries):
    poly = [df.loc[df['ADMIN'] == country]['geometry'].values[0]]
    stamen_terrain              = cimgt.StamenTerrain()
    # projections that involved
    st_proj = stamen_terrain.crs  #projection used by Stamen images
    ll_proj = ccrs.PlateCarree()  #CRS for raw long/lat
    # create fig and axes using intended projection
    fig = plt.figure(figsize=(8,9))
    ax = fig.add_subplot(122, projection=st_proj)
    ax.add_geometries(poly, crs=ll_proj, facecolor='none', edgecolor='black')
    pad1 = 0.5  #padding, degrees unit
    exts = [poly[0].bounds[0] - pad1, poly[0].bounds[2] + pad1, poly[0].bounds[1] - pad1, poly[0].bounds[3] + pad1];
    ax.set_extent(exts, crs=ll_proj)
    # make a mask polygon by polygon's difference operation
    # base polygon is a rectangle, another polygon is simplified switzerland
    msk = Polygon(rect_from_bound(*exts)).difference( poly[0].simplify(0.01) )
    msk_stm  = st_proj.project_geometry (msk, ll_proj)  # project geometry to the projection used by stamen
    # get and plot Stamen images
    ax.add_image(stamen_terrain, 8) # this requests image, and plot
    # plot the mask using semi-transparency (alpha=0.65) on the masked-out portion
    ax.add_geometries( msk_stm, st_proj, zorder=12, facecolor='white', edgecolor='none', alpha=0.65)
    ax.gridlines(draw_labels=True)
    plt.show()
我有的是分开的地图。我只需要一张他们的地图。 你能帮忙吗? 谢谢。

您根据工作改编的代码对单个国家都适用。如果多个相邻国家是新的目标,则需要选择所有这些国家并将其分解为一个几何体。只需要修改几行代码

示例:新的目标国家:[“挪威”、“瑞典”、“芬兰”]

需要替换的代码行:

poly = [df.loc[df['ADMIN'] == 'Switzerland']['geometry'].values[0]]
将其替换为以下代码行:

scan3 = df[ df['ADMIN'].isin(['Norway','Sweden', 'Finland']) ]
scan3_dissolved = scan3.dissolve(by='LEVEL')
poly = [scan3_dissolved['geometry'].values[0]]
你应该得到一个类似的图:


完美!!这就是我想做的。。。谢谢大家!@YOUSEFALBUHAISI正确的感谢方式是单击
接受答案
;)我还有一个问题,如何使用上述掩码屏蔽.nc文件?仅供参考。对于一个新用户,在你获得一些声誉积分之前,你不能投票支持我的答案。但您可以将其标记为“已接受”,并获得“+2”的声誉。