Geopandas 多张地图共享一个图例

Geopandas multiple maps sharing one legend

我想在网格中绘制同一个叶绿素地图的多个版本,这是我使用的代码:

fig, axes = plt.subplots(ncols=4, nrows=(len(list_decade)//4+1), figsize=(10,15))
i = 0
for dec in sorted(list_decade): #list_decade is a lost of decades [1900, 1910, 1920...]
  j = i%4 # column
  ax = axes[i//4, j]
  ax.title.set_text(f"{dec}'s")
  ax.axis("off")
  df_dec = df.query("decade == @dec") # df is a dataframe containing all data, I keep only the relevant decade

  df_dec.plot(column='nb_patentees', legend=True, edgecolor="black", figsize = (10,15), linewidth = 0.01, legend_kwds={'label': "Number of patentess (log)",
                          'orientation': "horizontal", 'shrink': 0.8}, ax = ax)
  i = i+1

结果

我的问题是我希望所有这些子图共享相同的图例,理想情况下,图例垂直位于图的右侧。 格

这是一个与您的示例相似的minimal reproducible example

In [4]: import numpy as np

In [5]: df = gpd.GeoDataFrame(
   ...:     {'val': range(6), 'cat': np.arange(6)//2},
   ...:     geometry=([g.Polygon([(-1, 0), (0, 0), (0, 1)]), g.Polygon([(0, 0), (1, 0), (0, -1)])])*3,
   ...: )

In [6]: df
Out[6]:
   val  cat                                           geometry
0    0    0  POLYGON ((-1.00000 0.00000, 0.00000 0.00000, 0...
1    1    0  POLYGON ((0.00000 0.00000, 1.00000 0.00000, 0....
2    2    1  POLYGON ((-1.00000 0.00000, 0.00000 0.00000, 0...
3    3    1  POLYGON ((0.00000 0.00000, 1.00000 0.00000, 0....
4    4    2  POLYGON ((-1.00000 0.00000, 0.00000 0.00000, 0...
5    5    2  POLYGON ((0.00000 0.00000, 1.00000 0.00000, 0....

In [7]: fig, axes = plt.subplots(ncols=3, nrows=1, figsize=(12, 4))
   ...: for i, c in enumerate(np.unique(df.cat.values)):
   ...:     ax = axes[i]
   ...:     df[df.cat == c].plot('val', ax=ax, legend=True)

使用matplotlib.colors.Normalize or another norm对齐配色方案:

In [15]: fig, axes = plt.subplots(ncols=3, nrows=1, figsize=(12, 4))
    ...: norm = matplotlib.colors.Normalize(vmin=df.val.min(), vmax=df.val.max())
    ...: for i, c in enumerate(np.unique(df.cat.values)):
    ...:     ax = axes[i]
    ...:     df[df.cat == c].plot('val', ax=ax, norm=norm, legend=True)
    ...: plt.show()