【问题标题】:Is it possible to plot multiple buffers in python是否可以在 python 中绘制多个缓冲区
【发布时间】:2022-09-23 16:56:15
【问题描述】:
如果我的问题很愚蠢,我对编码很抱歉,但我无法在任何地方找到解决方案。
我的问题是您是否可以在彼此之上绘制多个缓冲区,并使用多种颜色?我试图制作一张地图,我想要一个缓冲区显示距离坐标 20、30 和 50 公里的范围。到目前为止,我的尝试如下所示:
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]
标签:
python
buffer
geopandas
【解决方案1】:
我不知道你的问题到底是什么,因为我看不到你的情节 - 但你可以这样做
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()