Reputation: 435
I tried this example and it worked as shown in the link: GeoJSON issues with Plotly choropleth_mapbox.
Now I'm trying to use it in my Flask app without success and without any error message.
I prepared an example about what I'm doing:
import pandas as pd
import plotly.express as px
gjson={
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
1.398123838,
42.053625569
],
[
1.398415914,
42.053516811
],
[
1.398314792,
42.053358782
],
[
1.39808988,
42.053606388
],
[
1.398123838,
42.053625569
]
]
]
},
"properties": {
"id": 146,
"name": "17a"
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
1.403772968,
42.061291146
],
[
1.40386018,
42.061350579
],
[
1.403935831,
42.061408731
],
[
1.406719063,
42.060329611
],
[
1.406713442,
42.060322418
],
[
1.406712334,
42.060313576
],
[
1.403772968,
42.061291146
]
]
]
},
"properties": {
"id": 163,
"name": "6"
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
1.38425415,
42.041311214
],
[
1.384250799,
42.041343319
],
[
1.384284725,
42.141329127
],
[
1.38425415,
42.041311214
],
[
1.38425415,
42.041311214
]
]
]
},
"properties": {
"id": 164,
"name": "7a"
}
}
]
}
df = pd.DataFrame({'id':[146,163,164], 'name':['17a','6','7a'], 'value':[0.166639, 0.23465, 0.9343332]})
fig = px.choropleth_mapbox(df, geojson=gjson, color=df.value,
locations=df.name, featureidkey="properties.id",
center={"lat": 42.04951952053175, "lon": 1.3925319577334843},
mapbox_style="carto-positron", zoom=14)
fig.show()
When I put the json in geojson.io I can see the polygons:
However, fig.show() shows:
What an I doing wrong?
Upvotes: 1
Views: 1351
Reputation: 35115
This issue incorrectly associates geojson items with data frame items. If it is the name on the data frame side, the item on the geojson side will be the name. Also, if you want it to be the id of the geojson, the data frame side will be the id. Either way, the data format needs to be the same for both sides.
fig = px.choropleth_mapbox(df,
geojson=gjson,
color=df.value,
locations=df.name,
featureidkey="properties.name", # update
center={"lat": 42.04951952053175, "lon": 1.3925319577334843},
mapbox_style="carto-positron",
zoom=10
)
Upvotes: 1