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

如何基于GeoJSON属性通过geopandas自动设置轮廓与填充颜色?

利用GeoJSON属性中的stroke和fill字段为Geopandas多边形设置自定义颜色

我正在开发一个程序,用于从风暴预测中心(SPC)获取历史对流展望的GeoJSON数据,并通过geopandas进行绘图。当前代码已能在地图上正确绘制展望区域,但颜色显示不符合预期。我注意到SPC返回的GeoJSON的properties字段中包含了各分类的轮廓色(stroke)与填充色(fill)数据,示例如下:

{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 2, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "TSTM", "LABEL2": "General Thunderstorms Risk", "stroke": "#55BB55", "fill": "#C1E9C1"}}, {"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 3, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "MRGL", "LABEL2": "Marginal Risk", "stroke": "#005500", "fill": "#66A366"}}, {"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": ...}, "properties": {"DN": 4, "VALID": "202109010100", "EXPIRE": "202109011200", "ISSUE": "202109010042", "LABEL": "SLGT", "LABEL2": "Slight Risk", "stroke": "#DDAA00", "fill": "#FFE066"}}]}

请问是否可以利用properties中的stroke和fill数据,自动为每个MultiPolygon设置对应颜色?我的当前代码如下(假设所有包已导入):

outlook = "https://www.spc.noaa.gov/products/outlook/archive/2021/day1otlk_20210901_0100_cat.lyr.geojson"
world = geopandas.read_file(
    geopandas.datasets.get_path('naturalearth_lowres')
)
df = geopandas.read_file(outlook)
ax = world.plot(color='white', edgecolor='#333333',linewidth=0.3)
print(type(df))
s = geopandas.GeoDataFrame(df)
s.plot(ax=ax,markersize=0.7,figsize=(1000,1000))
ax.set_xlim(-140, -70) # focus on continental US
ax.set_ylim(25, 50) # focus on continental US
plt.savefig('outlook.jpg', dpi=360) # save as outlook.jpg

我查阅了geopandas文档,但未找到如何使用GeoJSON字段为多边形上色的方法。


解决方案

可以实现,geopandas的批量plot方法无法直接映射每个要素的独立颜色,但可以通过遍历GeoDataFrame的每一行,单独绘制每个多边形,并指定对应的facecolor(填充色)和edgecolor(轮廓色)。

修改后的代码如下:

import geopandas as gpd
import matplotlib.pyplot as plt

outlook = "https://www.spc.noaa.gov/products/outlook/archive/2021/day1otlk_20210901_0100_cat.lyr.geojson"
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
df = gpd.read_file(outlook)

# 绘制底图
ax = world.plot(color='white', edgecolor='#333333', linewidth=0.3, figsize=(12, 8))

# 遍历每个要素,使用properties中的颜色绘制
for idx, row in df.iterrows():
    # 提取填充色和轮廓色
    fill_color = row['fill']
    stroke_color = row['stroke']
    # 单独绘制当前多边形
    gpd.GeoSeries(row['geometry']).plot(
        ax=ax,
        facecolor=fill_color,
        edgecolor=stroke_color,
        linewidth=0.7  # 控制轮廓粗细,替代原代码的markersize(仅适用于点要素)
    )

# 设置地图范围
ax.set_xlim(-140, -70)
ax.set_ylim(25, 50)

# 保存图片
plt.savefig('outlook.jpg', dpi=360)
plt.close()

关键说明:

  • 遍历df的每一行,对每个多边形单独调用plot方法,实现每个要素的独立颜色配置。
  • 原代码中的markersize是点要素的参数,多边形的轮廓粗细需要用linewidth控制。
  • 通过gpd.GeoSeries(row['geometry'])将单个几何对象转换为GeoSeries,满足plot方法的调用要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:40:34