Search code examples
pythonbuffergeopandas

Is it possible to plot multiple buffers in python


Im rather new to coding so sorry if my question is stupid, but i can't find a solution anywhere. My question is if you can plot multiple buffers on top of eachother, with multiple colors? Im trying to make a map where i would like a buffer showing 20, 30 and 50km range from a coordinate. My try so far looks like this:

gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y), crs="EPSG:25832")

gdf30=gdf
gdf30['geometry'] = gdf30.geometry.buffer(30*1000)

gdf20=gdf
gdf20['geometry'] = gdf20.geometry.buffer(20*1000)

Map = geopandas.read_file("Map_DK_SWE.gpkg")
Map = Map.to_crs(25832)

fig,ax=plt.subplots()
Map.plot(ax=ax,color='white', edgecolor='black')
ax.set_ylim([6000000, 6500000])
ax.set_xlim([400000, 850000])

gdf30.plot(ax=ax, color='blue',zorder=2)
gdf20.plot(ax=ax, color='green',zorder=1)

[This is what i get from then code][1]

Solution

  • i dont know what exactly your issue is since I cant see your plot - but you can do it like this

    from matplotlib import pyplot as plt
    import geopandas as gpd
    
    cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    
    centroid = cities[cities.name == 'Tokyo']
    buffer_1 = cities[cities.name == 'Tokyo'].geometry.buffer(3)
    buffer_2 = cities[cities.name == 'Tokyo'].geometry.buffer(2)
    buffer_3 = cities[cities.name == 'Tokyo'].geometry.buffer(1)
    
    f, ax = plt.subplots()
    # plot basemap
    world.plot(edgecolor='k', facecolor='w', ax=ax)
    
    # plot buffers
    buffer_1.plot(color='r', label='buffer 1', ax=ax, alpha=.5)
    buffer_2.plot(color='b', label='buffer 2', ax=ax, alpha=.5)
    buffer_3.plot(color='g', label='buffer 3', ax=ax, alpha=.5)
    
    # plot original coordinates
    centroid.plot(marker='X', color='r', ax=ax)
    
    # crop map to extent
    ax.set_xlim(120, 145)
    ax.set_ylim(25, 50)
    plt.show()
    

    enter image description here