You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Geopandas中实现类似ggplot的按年份分面(Facet)功能?

在Geopandas中实现按年份分面的空间可视化方法

Geopandas本身没有像ggplot中facet_wrap那样的直接分面函数,但可以借助matplotlib子图或seaborn的FacetGrid实现类似的按年份分面的空间可视化效果,以下是两种常用方法:

方法一:Matplotlib手动创建子图分面

通过循环遍历年份,在matplotlib子图网格中逐个绘制对应年份的空间数据,灵活性更高:

import geopandas as gpd
import matplotlib.pyplot as plt

# 读取空间数据(假设数据包含geometry、winner、year字段)
usa_elections = gpd.read_file("your_data_source.shp")

# 获取排序后的唯一年份列表
years = sorted(usa_elections['year'].unique())
# 设置子图行数(与ggplot示例一致为4行)
n_rows = 4
# 计算所需子图列数
n_cols = (len(years) + n_rows - 1) // n_rows

# 创建子图网格
fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(16, 12))
# 扁平化坐标轴数组,方便循环操作
axes = axes.flatten()

# 定义获胜方对应的颜色映射
color_map = {'blue': 'blue', 'red': 'red'}  # 需与数据中winner字段值匹配

# 循环绘制每个年份的地图
for idx, year in enumerate(years):
    # 筛选当前年份的数据
    year_data = usa_elections[usa_elections['year'] == year]
    # 在对应子图绘制空间数据,设置填充色
    year_data.plot(
        column='winner',
        color=[color_map[win] for win in year_data['winner']],
        ax=axes[idx],
        legend=False
    )
    # 设置子图标题
    axes[idx].set_title(f'年份:{year}')
    # 隐藏坐标轴,让地图更整洁
    axes[idx].axis('off')

# 隐藏多余的空白子图(当年份数不足行列乘积时)
for empty_ax in axes[idx+1:]:
    empty_ax.axis('off')

# 调整子图间距
plt.tight_layout()
plt.show()

方法二:使用Seaborn的FacetGrid分面

借助Seaborn的FacetGrid可以更简洁地实现分面逻辑,无需手动管理子图:

import geopandas as gpd
import seaborn as sns
import matplotlib.pyplot as plt

usa_elections = gpd.read_file("your_data_source.shp")

# 定义颜色映射
color_map = {'blue': 'blue', 'red': 'red'}

# 创建FacetGrid,按year字段分面,设置每行最多4个图
g = sns.FacetGrid(usa_elections, col='year', col_wrap=4, height=3)

# 定义分面绘图函数
def plot_year_map(data, **kwargs):
    # 获取当前子图的坐标轴
    ax = plt.gca()
    # 绘制当前年份的空间数据
    data.plot(
        column='winner',
        color=[color_map[win] for win in data['winner']],
        ax=ax
    )
    # 隐藏坐标轴
    ax.axis('off')

# 将绘图函数应用到每个分面
g.map_dataframe(plot_year_map)

# 设置每个分面的标题
g.set_titles('年份:{col_name}')

plt.tight_layout()
plt.show()

以上两种方法都能实现与ggplot中facet_wrap类似的按年份分面效果,可根据需求选择更适配的方案。

内容的提问来源于stack exchange,提问作者Gevorg Atanesyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:10:29