创建 Plotly Choropleth 时出现 ValueError
ValueError when creating Plotly Choropleth
我正在尝试使用 plotly express 创建一个叶绿素图表。我有两个文件,我的 geojson 文件和我的数据文件。下面我的 geojson 文件中一个国家/地区的示例片段:
{'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 的头部如下所示,其中包含将用于创建热图的列 'data'
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
下面的代码片段是我试图执行以绘制我的等值线图的代码
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`
关于可能导致此错误的原因有什么想法吗?谢谢!
要解决您的问题,您需要将数据框的 ID 值绑定到 geojson 值的 ISO_A3 值。 aruba在意大利修改为ABW for ITA,得到地图输出
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()
我正在尝试使用 plotly express 创建一个叶绿素图表。我有两个文件,我的 geojson 文件和我的数据文件。下面我的 geojson 文件中一个国家/地区的示例片段:
{'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 的头部如下所示,其中包含将用于创建热图的列 'data'
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 |
下面的代码片段是我试图执行以绘制我的等值线图的代码
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`
关于可能导致此错误的原因有什么想法吗?谢谢!
要解决您的问题,您需要将数据框的 ID 值绑定到 geojson 值的 ISO_A3 值。 aruba在意大利修改为ABW for ITA,得到地图输出
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()