Hi all,
I am having a problem with choropleth_mapbox in python. I can easily make a new choropleth_mapboy figure as follows
import geopandas as gpd
import requests
import plotly_express as px
from shapely.geometry import Point
# import data
data = json.loads(requests.get("http://drive.google.com/uc?id=1ex0kKZgEuhF6-92HyKLW8J1U0r_gxmDM").json())
gdf = gpd.GeoDataFrame.from_features(data["features"]).set_geometry("geometry")
# create new choropleth_mapbox
fig=px.choropleth_mapbox( gdf,
geojson=gdf.geometry,
locations=gdf.index,
color="dummy",
center={"lon": gdf.iat[0,0].centroid.x, "lat":gdf.iat[0,0].centroid.y},
mapbox_style="carto-darkmatter",
color_continuous_scale=blues,
zoom=13.5
)
and fig.show() displays the figure exactly as expected.
But if I create a figure first:
# create figure first
dummy_df = gpd.GeoDataFrame({"geometry":[Point(7.9797197,47.32547143)], "dummy":[1]})
fig=px.choropleth_mapbox( dummy_df,
geojson=dummy_df.geometry,
locations=dummy_df.index,
color="dummy",
center={"lon": gdf.iat[0,0].centroid.x, "lat":gdf.iat[0,0].centroid.y},
mapbox_style="carto-darkmatter",
color_continuous_scale=blues,
zoom=13.5
)
fig.show()
and then add the layer as follows:
fig.add_choroplethmapbox(geojson=gdf.geometry, locations=gdf.index)
I get a TypeError βobject of type polygon is not JSON serialisableβ
What am I doing wrong?
I have also tried add_trace(px.choropleth_mapbox())
but this throws a different error.