我正在尝试使用创建一个 choroplethplotly.express
。该图能够加载,但它只显示一种颜色。我可以将鼠标悬停在每个功能上,它会显示相关信息,但不以可变颜色显示。这意味着它正在读取 geojson 但无法正确显示。u/geds133 也有同样的问题 https://stackoverflow.com/questions/61966708/plotly-express-choropleth-only-showing-some-information-from-geojson,但由于声誉较低,我无法联系他们或发表评论。
这是我的“预测”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
参数来指定匹配值的位置locations
如中所写the docs https://plotly.com/python/choropleth-maps/#choropleth-maps-with-gochoropleth。这是我用来创建分区统计图的代码:
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()
And here is what the output shows. Note that mouse-over shows relevant information:
我的 geojson 和 csv 可供下载here https://drive.google.com/file/d/1yUBsWTeOUbKe8kkNQlE_tUKBsO_fV5h0/view?usp=sharing.