Reputation: 654
I'm working with a geodataframe of a city where every unit is a district (administrative division). Its plot looks like this:
import geopandas as gpd
df = gpd.read_file('districts_lima.geojson')
df.plot()
Then, I'm merging some geographic units into larger groups, using a variable named zone
. The result is:
df2 = df.dissolve(by='zone', aggfunc='sum')
df2.plot(column='population', legend=True, cmap='Blues')
My only problem is that when I reproduce the same plot with darker borders, it becomes evident that some of the merged polygons (zones) have inner lines, which are inner district borders from the original geodataframe. This is shown clearly in this plot:
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='black')
Is there a way to use geopandas to delete the inner lines of the polygons so they wouldn't appear in the last plot?
My data can be found here.
Upvotes: 5
Views: 3838
Reputation: 654
I actually found a good solution that pertains specifically to the fact that my issue is being created after applying the dissolve()
property of geopandas
. Apparently, the problem was generated by unnoticeable differences in the borderlines of contiguous inner units which prevented the collapsing to delete the interior lines of the resulting polygons.
This is solved by adding a small buffer to widen the polygon lines so those unnoticeable differences are removed and every inner line of the initial polygons actually overlap. Specifically, I needed to add:
df2['geometry'] = df2['geometry'].buffer(0.0001)
before
df2 = df.dissolve(by='zone', aggfunc='sum')
So now the plot command
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='Black')
yields:
Upvotes: 9
Reputation: 18782
Use axes (ax1) to enable plotting several layers on a common axes. Also use zorder, to arrange the layers, higher values put the layer above the ones with lower values.
fig, ax1 = plt.subplots(1, 1)
...
# bottom layer (has thick black lines)
df2.plot(column='population', legend=True, cmap='Blues', edgecolor='black', ax=ax1, zorder=5, lw=6)
# top layers (thin boundary lines)
df2.plot(column='population', legend=True, cmap='Blues', ax=ax1, zorder=6)
Hopefully the top layer will hide almost all the bottom layer, but partial external boundary lines.
Upvotes: 1