I found the solution to this question: The locations and center arguments need to be filled. The following code works.
fig = px.choropleth_mapbox(
df,
geojson=gdf_json,
color=df['Number_Neddle'],
featureidkey="properties.neighbourh",
locations=df['neighbourh'],
mapbox_style="carto-positron",
center = {"lat": 53.543727, "lon": -113.494654},
zoom=10.5
)
fig.show()
Thank you for whoever helps.I am really frustrated.
I have been trying to build a choropleth map using plotly choropleth_mapbox. But it always shows as blank.
I have one dataframe with two columns.
df=pd.read_csv('Downloads/Neighbourhood/neighbourhood.csv')
df.head()
neighbourh Number_Neddle
0 1010.0 90
1 1020.0 46
2 1030.0 1559
3 1070.0 50
4 1090.0 463
neighbourh is the neighbourhood id and number_neddle is the number of needles collected in that neighbourhood.
Then I have a json file transferred using shapefiles.
with open ('Downloads/Neighbourhood/neighbourhood.json') as f:
gdf_json =json.load(f)
gdf_json["features"][0]["properties"].keys()
>>> gdf_json["features"][0]["properties"].keys()
dict_keys(['neighbourh', 'descriptiv', 'descriptio'])
then, I tried to design the map.
fig = px.choropleth_mapbox(
df,
geojson=gdf_json,
color=df['Number_Neddle'],
featureidkey="properties.neighbourh",
mapbox_style="carto-positron"
)
fig.show()
The result I get is:
