基于数据框的列绘制多个地图以使用 Folium 进行比较
Draw multiple maps based on columns of a dataframe to compare using Folium
我有一个包含几何和 7 个属性字段的地理数据框。我想以 table 格式创建 7 个地图,每个地图代表一列的值。我可以为一张地图做到这一点,但我不知道如何遍历所有列。
|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...
A 列和 B 列是 ID。我使用以下代码创建了一个基于“C”列的地图。我想要 7 张地图,第一行 4 张,第二行 3 张。
无花果, 斧头 = plt.subplots(无花果大小=(12, 12))
minMax_gdf.plot(column='C',
ax=ax,
legend=True,
legend_kwds={'label': "Total C",
'orientation': "vertical"})
也许这个例子会对你有所帮助 - 我只是循环遍历列名:
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])
create_axes_2_rows()
函数是在Position 5 subplots in Matplotlib的基础上创建网格来定位地块数量不均匀的地块。
我有一个包含几何和 7 个属性字段的地理数据框。我想以 table 格式创建 7 个地图,每个地图代表一列的值。我可以为一张地图做到这一点,但我不知道如何遍历所有列。
|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...
A 列和 B 列是 ID。我使用以下代码创建了一个基于“C”列的地图。我想要 7 张地图,第一行 4 张,第二行 3 张。
无花果, 斧头 = plt.subplots(无花果大小=(12, 12))
minMax_gdf.plot(column='C',
ax=ax,
legend=True,
legend_kwds={'label': "Total C",
'orientation': "vertical"})
也许这个例子会对你有所帮助 - 我只是循环遍历列名:
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])
create_axes_2_rows()
函数是在Position 5 subplots in Matplotlib的基础上创建网格来定位地块数量不均匀的地块。