0

I have a geodataframe with geometry and 7 attribute fields. I would like to create 7 maps in a table format each representing values of one column. I could do it for one map, but I do not know how to loop through all the columns.

|A| B|  C|  D|  F|  E|  G|  H|  W|  geom|
|0| 5350128.04| 0.494195|   0.411917|   0.265306|   0.124506|   0.263811|   0.400000|   0.166342|   0.103118|   MULTIPOLYGON (((7223015.300 933307.794, 722283...
|1| 5350363.06| 0.182385|   0.233161|   0.102041|   0.158103|   0.162909|   0.404255|   0.388132|   0.393285|   MULTIPOLYGON (((7232843.491 944100.077, 723282...
|2  5350363.07| 0.182385|   0.233161|   0.102041|   0.158103|   0.162909|   0.285106|   0.455253|   0.400480|   MULTIPOLYGON (((7234559.454 946413.506, 723455...
|3| 5350378.23| 0.182614|   0.240933|   0.163265|   0.081028|   0.286922|   0.417021|   0.148833|   0.122302|   MULTIPOLYGON (((7231013.429 945703.934, 723100...
|4| 5350378.24| 0.182614|   0.240933|   0.163265|   0.081028|   0.286922|   0.740426|   0.305447|   0.191847|   MULTIPOLYGON (((7231620.303 946337.383, 723161...

Columns A and B are Ids. I used the following code to create a map based on "C" column. I would like to have 7 maps, 4 in first row and 3 in the second.

fig, ax = plt.subplots(figsize=(12, 12))

minMax_gdf.plot(column='C',
           ax=ax,
           legend=True,
           legend_kwds={'label': "Total C",
                        'orientation': "vertical"})
GeoBeez
  • 920
  • 2
  • 12
  • 20

1 Answers1

1

Maybe this example would help you - I am simply looping through column names:

import geopandas
import matplotlib.pyplot as plt

#preparing mock data
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world['gdp_per_pop_est'] = world['gdp_md_est'] / world['pop_est']
world[['random_1', 'random_2', 'random_3', 'random_4']] = np.random.randint(0,100,size=(len(world), 4))

#function to create plots grid
def create_axes_2_rows(n: int):
    if (n%2 == 0):
        return [plt.subplot2grid(shape=(2,n//2), loc=(0,i)) for i in range(0, n//2)] \
            + [plt.subplot2grid(shape=(2,n//2), loc=(1,i)) for i in range(0, n//2)]
    else:
        max_n = n + (n%2)
        return [plt.subplot2grid(shape=(2,max_n), loc=(0,i), colspan=2) for i in range(0, max_n, 2)] \
            + [plt.subplot2grid(shape=(2,max_n), loc=(1,i), colspan=2) for i in range(1, n, 2)]

#actual plotting
columns_to_plot = ['pop_est', 'gdp_md_est', 'gdp_per_pop_est', 'random_1', 'random_2', 'random_3', 'random_4']
fig = plt.figure(figsize=(20,6))
axes = create_axes_2_rows(len(columns_to_plot))
for i, column in enumerate(columns_to_plot):
    world.plot(column=column, ax=axes[i])

Maps

create_axes_2_rows() function is based on Position 5 subplots in Matplotlib to create grid to position the plots for uneven number of plots.

Daniel Wlazło
  • 1,105
  • 1
  • 8
  • 17