【问题标题】:Automatically set outline and fill color based on GeoJSON properties with geopandas使用 geopandas 根据 GeoJSON 属性自动设置轮廓和填充颜色
【发布时间】:2023-02-07 06:54:51
【问题描述】:

我正在制作一个程序,从风暴预测中心 (SPC) 过去的对流前景中检索 GeoJSON 数据,并使用 geopandas 绘制它。使用我当前的代码,它能够将 outlooks 正确地绘制到地图上。但是,着色不正确。我注意到 SPC 返回的 GeoJSON 包含类别的轮廓和填充着色数据 -(在 properties 字段中)

{"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中的strokefill数据自动给每一个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 中的字段为多边形着色。

【问题讨论】:

    标签: python matplotlib geojson geopandas


    【解决方案1】:

    你是一个夸格远离最终结果。 plot 接受 Series 作为 color 的参数:

    colorstr,np.array,pd.系列(def: None) 如果指定,所有对象 将统一着色。

    s.plot(ax=ax, color=s["fill"], markersize=0.7, figsize=(1000,1000)) # <-  color=s["fill"] (added here)
    

    输出 :

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 2013-09-23
      • 2013-04-23
      • 1970-01-01
      • 2021-06-21
      相关资源
      最近更新 更多