【问题标题】:geopandas .explore - How to set marker icon?geopandas .explore - 如何设置标记图标?
【发布时间】:2022-08-15 01:04:13
【问题描述】:

我想将 .explore 中标记的图标更改为房屋图标。

我已经阅读了geopandas.GeoDataFrame.explorefolium 的文档,仍然无法理解。

geo_df.explore(m=m
                    ,column=\'pop\'
                    ,tooltip={\"name\",\"pop\"}     
                    ,cmap=\'summer\'
                    ,style_kwds=dict(stroke=True,weight=1,color=\'black\', opacity=0.5, fillOpacity=0.9)
                    ,marker_kwds=dict(radius=5,icon=folium.Icon(icon=\'house-blank\'))              
                    ,name=\"Residental\"
                    )

图标代表房子的位置,我根据整数值设置颜色图。

另外,有没有办法使缩放时半径大小不改变?

  • 房子的图标不是“家”吗?我不知道它们是否都可用,但请参阅this 以获取图标列表。 icon=folium.Icon(icon=\'home\')
  • 我不确定在使用自定义标记时是否仍然可以使用cmap,但为了使用folium 标记,您必须通过设置marker_type=\'marker\' 告诉geopandas 这样做:geo_df.explore(m=m ,column=\'pop\' ,marker_type=\'marker\' ,marker_kwds=dict(radius=5,icon=folium.Icon(icon=\'house-blank\')) )
  • 似乎只接受folium.map.Icons:geo_df.explore(m=m, marker_type=\'marker\', marker_kwds={\'icon\': folium.map.Icon(icon=\'home\', prefix=\'fa\')})

标签: python geopandas marker folium


【解决方案1】:

有一个特点在GeoJson Marker 和一个功能大熊猫flexible style function 可用于满足您的要求

  1. 使用城市数据集使您的代码成为 MWE
  2. 定义 marker_typemarker_kwds 以便分区图标用来
  3. 定义一个风格功能这样标记就可以根据需要进行格式化。实际使用由创建的列大熊猫来自columncmap 参数
  4. 标记的大小不会随着缩放而改变字体大小已设置为像素
    import geopandas as gpd
    import folium
    import numpy as np
    
    geo_df = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
    geo_df["pop"] = np.random.randint(1, 5, len(geo_df))
    m = None
    
    m = geo_df.explore(
        m=m,
        column="pop",
        tooltip={"name", "pop"},
        cmap="summer",
        style_kwds=dict(
            style_function=lambda x: {
                "html": f"""<span   class="fa fa-home" 
                                    style="color:{x["properties"]["__folium_color"]};
                                    font-size:14px"></span>"""
            },
        ),
        marker_type="marker",
        marker_kwds=dict(icon=folium.DivIcon()),
        name="Residental",
        height=300,
        width=300,
    )
    
    m
    

    输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2019-11-22
    • 2019-10-11
    • 2019-10-09
    • 1970-01-01
    • 2016-03-03
    • 2019-09-22
    相关资源
    最近更新 更多