Reputation: 65
I want to plot multiple version of the same chloropeth map in a grid, here is the code that I use:
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
And the result
My problem is that I would like all these subplot to share the same legend which ideally would be located at the right of the figure, vertically. ge
Upvotes: 0
Views: 1144
Reputation: 15442
Here's a minimal reproducible example similar to your 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)
Use matplotlib.colors.Normalize
or another norm to align the color scheme:
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()
Upvotes: 1