【问题标题】:How to show different states of India using choropleth plotly map如何使用等值线图显示印度的不同州
【发布时间】:2022-08-20 19:31:04
【问题描述】:

我正在尝试使用类似于美国的情节来绘制印度地图 [在此处输入图片描述][1]

 fig_choropleth = px.choropleth(dff, locations=\'state\', geojson=india_states, featureidkey=\'properties.ST_NM\',locationmode=\'geojson-id\', color=\'content_view\',scope=\'asia\')

它显示了整个亚洲范围

[我希望印度像美国一样具有不同的国家][2] fig_choropleth.update_geos(fitbounds=\"geojson\", visible=False)

如果我在上面的代码之外使用它,结果如下

[在此处输入图片描述][3] [1]:https://i.stack.imgur.com/bBWUm.png [2]:https://i.stack.imgur.com/bGtJE.png [3]:https://i.stack.imgur.com/UBEta.png

    标签: plotly plotly-dash


    【解决方案1】:

    您的图像未在 SO 中显示。你想要的代码:

    # restrict to just India
    fig_choropleth.update_geos(fitbounds="locations", visible=False)
    

    您已经注意到如何确保显示所有状态。这可以通过构建基本轨迹然后添加状态子集作为 Choropleth 来完成

    带有印度州 geojson 和模拟数据框的完整 MWE

    import geopandas as gpd
    import pandas as pd
    import numpy as np
    import plotly.express as px
    
    # get some geojson for India.  Reduce somplexity of geomtry to make it more efficient
    url = "https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
    gdf = gpd.read_file(url)
    gdf["geometry"] = gdf.to_crs(gdf.estimate_utm_crs()).simplify(1000).to_crs(gdf.crs)
    india_states = gdf.rename(columns={"NAME_1": "ST_NM"}).__geo_interface__
    
    # simulate data frame
    dff = pd.DataFrame(
        {
            "state": ['Andaman and Nicobar', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh', 'Chhattisgarh', 'Dadra and Nagar Haveli', 'Daman and Diu', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Lakshadweep', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Orissa', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Tripura', 'Uttar Pradesh', 'Uttaranchal', 'West Bengal'],  # fmt: skip
            "content_view": np.random.randint(1, 5, 35),
        }
    )
    
    # data frame only has a subset of states...
    dff = dff.sample(20)
    
    # create base map of all India states
    fig_choropleth = px.choropleth(
        pd.json_normalize(india_states["features"])["properties.ST_NM"],
        locations="properties.ST_NM",
        geojson=india_states,
        featureidkey="properties.ST_NM",
        color_discrete_sequence=["lightgrey"],
    )
    
    
    # users code to generate choropleth
    fig_choropleth.add_traces(
        px.choropleth(
            dff,
            locations="state",
            geojson=india_states,
            featureidkey="properties.ST_NM",
            locationmode="geojson-id",
            color="content_view",
            scope="asia",
        ).data
    )
    
    # restrict to just India
    fig_choropleth.update_geos(fitbounds="locations", visible=False)
    fig_choropleth
    

    输出

    【讨论】:

    • 谢谢 Rob,我对位置 =state 有疑问,在我们的代码中选择了所有状态,因此状态被突出显示,如果少数状态不在数据框中,它将不会像美国案例一样突出显示和显示
    • 更新....构建一个基础层,然后向它添加跟踪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    相关资源
    最近更新 更多