【问题标题】:ValueError when creating Plotly Choropleth创建 Plotly Choropleth 时出现 ValueError
【发布时间】:2021-01-12 01:26:36
【问题描述】:

我正在尝试使用 plotly express 创建一个叶绿体图表。我有两个文件,我的 geojson 文件和我的数据文件。下面我的 geojson 文件中一个国家的示例 sn-p:

{'type': 'Feature',
 'properties': {'ADMIN': 'Aruba', 'ISO_A3': 'ABW'},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-69.99693762899992, 12.577582098000036],
    [-69.93639075399994, 12.53172435100005],
    [-69.92467200399994, 12.519232489000046],
    [-69.91576087099992, 12.497015692000076],
    [-69.88019771999984, 12.453558661000045],
    [-69.87682044199994, 12.427394924000097],
    [-69.88809160099993, 12.417669989000046],
    [-69.90880286399994, 12.417792059000107],
    [-69.93053137899989, 12.425970770000035],
    [-69.94513912699992, 12.44037506700009],
    [-69.92467200399994, 12.44037506700009],
    [-69.92467200399994, 12.447211005000014],
    [-69.95856686099992, 12.463202216000099],
    [-70.02765865799992, 12.522935289000088],
    [-70.04808508999989, 12.53115469000008],
    [-70.05809485599988, 12.537176825000088],
    [-70.06240800699987, 12.546820380000057],
    [-70.06037350199995, 12.556952216000113],
    [-70.0510961579999, 12.574042059000064],
    [-70.04873613199993, 12.583726304000024],
    [-70.05264238199993, 12.600002346000053],
    [-70.05964107999992, 12.614243882000054],
    [-70.06110592399997, 12.625392971000068],
    [-70.04873613199993, 12.632147528000104],
    [-70.00715084499987, 12.5855166690001],
    [-69.99693762899992, 12.577582098000036]]]},
 'id': 'ABW'}

来自 df 的头部如下所示,其中包含将用于创建热图的“数据”列

Location_Site Country City Cluster_Name Market_Type data id
2 IT-MIL Italy Milan Italy Mature 73.14% ITA
3 ES-MAD Spain Madrid Iberia Mature 55.27% ESP
4 PT-LIS Portugal Lisbon Iberia Medium 45.71% PRT
5 AE-DXB United Arab Emirates Dubai EMEA Emerging Markets (EEM) Emerging 62.98% ARE
6 EG-CAI Egypt Cairo EMEA Emerging Markets (EEM) Emerging 20.36% EGY

下面的代码 sn-p 是我试图执行以绘制我的等值线图

fig = px.choropleth(df,
                    locations = 'id',
                    geojson = data, 
                    color = 'data')
fig.show()

执行后我收到以下错误:

ValueError: The first argument to the plotly.graph_objs.layout.Template 
constructor must be a dict or 
an instance of :class:`plotly.graph_objs.layout.Template`

关于可能导致此错误的任何想法?谢谢!

【问题讨论】:

    标签: python plotly choropleth


    【解决方案1】:

    要解决您的问题,您需要将数据框的 ID 值绑定到 geojson 值的 ISO_A3 值。 aruba 修改为 ABW for ITA in Italy,得到地图的输出。

    import plotly.express as px
    
    geo_data = {'type': 'Feature',
     'properties': {'ADMIN': 'Aruba', 'ISO_A3': 'ABW'},
     'geometry': {'type': 'Polygon',
      'coordinates': [[[-69.99693762899992, 12.577582098000036],
        [-69.93639075399994, 12.53172435100005],
        [-69.92467200399994, 12.519232489000046],
        [-69.91576087099992, 12.497015692000076],
        [-69.88019771999984, 12.453558661000045],
        [-69.87682044199994, 12.427394924000097],
        [-69.88809160099993, 12.417669989000046],
        [-69.90880286399994, 12.417792059000107],
        [-69.93053137899989, 12.425970770000035],
        [-69.94513912699992, 12.44037506700009],
        [-69.92467200399994, 12.44037506700009],
        [-69.92467200399994, 12.447211005000014],
        [-69.95856686099992, 12.463202216000099],
        [-70.02765865799992, 12.522935289000088],
        [-70.04808508999989, 12.53115469000008],
        [-70.05809485599988, 12.537176825000088],
        [-70.06240800699987, 12.546820380000057],
        [-70.06037350199995, 12.556952216000113],
        [-70.0510961579999, 12.574042059000064],
        [-70.04873613199993, 12.583726304000024],
        [-70.05264238199993, 12.600002346000053],
        [-70.05964107999992, 12.614243882000054],
        [-70.06110592399997, 12.625392971000068],
        [-70.04873613199993, 12.632147528000104],
        [-70.00715084499987, 12.5855166690001],
        [-69.99693762899992, 12.577582098000036]]]},
     'id': 'ABW'}
    
    import pandas as pd
    import numpy as np
    import io
    
    data = '''
     Location_Site Country City Cluster_Name Market_Type data id 
    2 IT-MIL Italy Milan Italy Mature 73.14% ABW 
    3 ES-MAD Spain Madrid Iberia Mature 55.27% ESP 
    4 PT-LIS Portugal Lisbon Iberia Medium 45.71% PRT 
    5 AE-DXB "United Arab Emirates" Dubai "EMEA Emerging Markets (EEM)" Emerging 62.98% ARE 
    6 EG-CAI Egypt Cairo "EMEA Emerging Markets (EEM)" Emerging 20.36% EGY 
    '''
    
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    
    fig = px.choropleth(df,
                        locations = 'id',
                        geojson = geo_data, 
                        featureidkey="properties.ISO_A3",
                        color = 'data')
    fig.show()
    

    【讨论】:

    • 很高兴能为您提供帮助。请接受我的回答。
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2017-09-27
    • 2018-06-13
    • 1970-01-01
    • 2022-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多