【问题标题】:Add a border around parts of a region, matplotlib/geopandas在区域的某些部分周围添加边框,matplotlib/geopandas
【发布时间】:2022-01-21 13:20:13
【问题描述】:

我会有一张显示斯德哥尔摩市镇的地图。如下所示。


fig, ax = plt.subplots(1, figsize=(4, 4))

matplotlib.rcParams["figure.dpi"] = 250
ax.axis('off')

ax1 = geo_df1.plot(edgecolor='black', column=geo_df1.rel_grp, cmap=my_cmp, linewidth=0.3, ax=ax, categorical=True)#, 

plt.show(ax1)

我想在东边添加一个放大的边框。像这样的东西。如何在 matplotlib 中做到这一点?

【问题讨论】:

    标签: python matplotlib geometry geopandas


    【解决方案1】:
    • 问题不包括几何,所以有来源
    • 这是一个绘制东边LineString 的简单案例。为示例目的生成了一个
    import requests
    import geopandas as gpd
    import shapely.ops
    import shapely.geometry
    res = requests.get("http://data.insideairbnb.com/sweden/stockholms-län/stockholm/2021-10-29/visualisations/neighbourhoods.geojson")
    
    # get geometry of stockholm
    gdf = gpd.GeoDataFrame.from_features(res.json()).set_crs("epsg:4326")
    
    # plot regions of stockholm
    ax = gdf.plot()
    
    # get linestring of exterior of all regions in stockhold
    ls = shapely.geometry.LineString(shapely.ops.unary_union(gdf["geometry"]).exterior.coords)
    b = ls.bounds
    # clip boundary of stockholm to left edge
    ls = ls.intersection(shapely.geometry.box(*[x-.2 if i==2 else x  for i,x in enumerate(b)]))
    
    # add left edge to plot
    gpd.GeoSeries(ls).plot(edgecolor="yellow", lw=5, ax=ax)
    
    
    

    【讨论】:

    • 谢谢!我会查看您的代码,非常感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 2013-08-07
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    相关资源
    最近更新 更多