Home > Enterprise >  Add a border around parts of a region, matplotlib/geopandas
Add a border around parts of a region, matplotlib/geopandas

Time:12-20

I'll have a map showing the municipalities of Stockholm. Displayed below. enter image description here


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)

I want to add an amplified border to the east. Something like this. How can I do this in matplotlib?

enter image description here

CodePudding user response:

  • question does not include geometry, so have sourced
  • it's a simple case of plotting a LineString that is the eastern edge. Have generated one for purpose of example
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)


enter image description here

  • Related