1

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:

enter image description here

However, fig.show() shows:

enter image description here

What an I doing wrong?

Mauro Assis
  • 375
  • 1
  • 5
  • 22
  • According to the Plotly docs, the `geojson` argument expects a "GeoJSON-formatted dict": https://plotly.github.io/plotly.py-docs/generated/plotly.express.choropleth_mapbox.html. Your code passes in the multi-line string `gjson`. Try converting the string to JSON with `import json` near the top of your script, and `gjson = json.loads(gjson)` – Peter Leimbigler Sep 19 '21 at 18:44
  • Tks Peter, however, it doesn't work. The result was the same: nothing appears, no error message. – Mauro Assis Sep 19 '21 at 19:48
  • I edited my example to include Peter suggestion, as is necessary to accomplish the plotly documentation. Now the geojson argument is a "GeoJSON-formatted dict". – Mauro Assis Sep 19 '21 at 20:21

1 Answers1

1

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
                          )

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32