Plotly.express 等值线只显示一种颜色

Plotly.express choropleth only shows one color

我正在尝试使用 plotly.express 创建等值线。该图能够加载,但它只显示一种颜色。我可以将鼠标悬停在每个功能上,它会显示相关信息,但不会以可变颜色显示。这意味着它正在读取 geojson 但未正确显示。 u/geds133 had the same issue,但由于声誉不佳,我无法联系他们或发表评论。

这是我的“预测” df:

import pandas as pd
predictions = pd.read_csv("Predictions_DF_2002.csv")

predictions.head()

huc12           Predicted PRBT   Std
170102120304    30.677075        23.348831
170102120603    31.362211        23.784001
90400010201     5.697461         7.688427
100301040401    3.493039         5.36472
170101011208    4.421055         11.924093

我正在尝试将 DataFrame 与 geojson 文件中的 属性 匹配:

#Read in geojson
import geopandas as gpd
import json


hucs = gpd.read_file(~/"HUC.geojson")

#Populate hucs['properties'] (i.e. convert to plotly-readible geojson-type)
hucs = json.loads(hucs.to_json())

#Print Properties for sanity check
print(hucs['features'][0]['properties'])
#...<a bunch of stuff we don't care about> 
{'huc12':170102120304}
#...

因此,我可以使用 featureidkey 参数来指定在何处匹配 the docs 中所写的 locations 的值。这是我用来创建等值线的代码:

fig = px.choropleth(predictions,
                    geojson=hucs, color='Predicted PRBT',
                    locations='huc12', featureidkey='properties.huc12',
                    color_continuous_scale="Viridis", labels={'Predicted PRBT':'Predicted % RBT'})
fig.update_geos(fitbounds="locations",visible=False)
fig.show()

这是输出显示的内容。请注意,鼠标悬停会显示相关信息:

我的 geojson 和 csv 可供下载 here

绘图前必须展开 geojson:

#Read in geojson
import geopandas as gpd
import json


hucs = gpd.read_file(~/"HUC.geojson")

#Populate hucs['properties'] (i.e. convert to plotly-readible geojson-type)
hucs = json.loads(hucs.to_json())

from geojson_rewind import rewind
hucs_rewound = rewind(hucs,rfc7946=False)


fig = px.choropleth(predictions,
                    geojson=hucs_rewound, color='Predicted PRBT',
                    locations='huc12', featureidkey='properties.huc12',
                    color_continuous_scale="Viridis", labels={'Predicted PRBT':'Predicted % RBT'})
fig.update_geos(fitbounds="locations",visible=False)
fig.show()

https://github.com/plotly/plotly.py/issues/2354#issuecomment-638742767https://github.com/plotly/plotly.py/issues/2619