Search code examples
pythongeopandas

Looping to plot multiple map with geopandas and removing axis


I would like to create a png that combines a similar chloropeth map of the UK at different subperiod. My input dataframe (df) has the following struture:

decade nb_patentees area
1910 25 zone1
1910 15 zone2
1920 34 zone1
1920 35 zone2

Here is what I do:

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

It works ok except when the number of subfigures does not match the square of cols x rows in which case I got an empty map with axes. Do you know how can I get rid of it?

My result


Solution

  • This should remove the axis on your blank subplot:

    axes[-1, -2].axis('off')