Geopandas 绘制为子图

Geopandas plots as subfigures

假设我有以下包含 3 个多边形对象的地理数据框。

import geopandas as gpd
from shapely.geometry import Polygon

p1=Polygon([(0,0),(0,1),(1,1),(1,0)])
p2=Polygon([(3,3),(3,6),(6,6),(6,3)])
p3=Polygon([(3,.5),(4,2),(5,.5)])

gdf=gpd.GeoDataFrame(geometry=[p1,p2,p3])
gdf['Value1']=[1,10,20]
gdf['Value2']=[300,200,100]

gdf内容:

>>> gdf
                               geometry  Value1  Value2
0   POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))       1     300
1   POLYGON ((3 3, 3 6, 6 6, 6 3, 3 3))      10     200
2  POLYGON ((3 0.5, 4 2, 5 0.5, 3 0.5))      20     100
>>> 

我可以通过调用 geopandas.plot() 两次来为每个图制作单独的图形。但是,有没有办法让我在同一个图中将这两个地图彼此相邻地绘制为子图?

Always always always 提前创建您的 matplotlib 对象并将它们传递给绘图方法(或直接使用它们)。这样做,您的代码将变为:

from matplotlib import pyplot
import geopandas
from shapely import geometry

p1 = geometry.Polygon([(0,0),(0,1),(1,1),(1,0)])
p2 = geometry.Polygon([(3,3),(3,6),(6,6),(6,3)])
p3 = geometry.Polygon([(3,.5),(4,2),(5,.5)])

gdf = geopandas.GeoDataFrame(dict(
        geometry=[p1, p2, p3],
        Value1=[1, 10, 20],
        Value2=[300, 200, 100],
))

fig, (ax1, ax2) = pyplot.subplots(ncols=2, sharex=True, sharey=True)
gdf.plot(ax=ax1, column='Value1')
gdf.plot(ax=ax2, column='Value2')

这给了我:

// for plotting multiple GeoDataframe 
import geopandas as gpd

gdf = gpd.read_file(geojson) 
fig, axes = plt.subplots(1,4, figsize=(40,10))
axes[0].set_title('Some Title')
gdf.plot(ax=axes[0], column='Some column for coloring', cmap='coloring option')

axes[0].set_title('Some Title')
gdf.plot(ax=axes[0], column='Some column for coloring', cmap='coloring option')