Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Problem Of Adding Shape to Maps

Hi

I want to add a shape to my maps figure, But the result of below code is something else.

import plotly.express as px

df = px.data.election()


fig = px.choropleth_mapbox(df, 
                           locations="district", featureidkey="properties.district",
                           center={"lat": 0.0, "lon": 0.0},
                           mapbox_style="carto-positron", zoom=2)

fig.add_shape(type="circle", xref="x", yref="y",
    fillcolor="PaleTurquoise", x0=0, y0=0, x1=0+.01, y1=0+.01,
    line_color="black",opacity=0.1)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

What should I do to add a shape with the diameter that I what in the location that I want?

@Bijan
Such a small circle can be added with a go.Scattermapbox trace, with markers at the circle centers.
But a circle of arbitrary radius is better defined as a mapbox layer.

layers=[dict(sourcetype = 'geojson',
             source =shape,
             below=' ', 
             type = 'fill',   
             color = 'PaleTurquoise',
             opacity=0.8
)]

where shape is not defined as in the case of a trace referenced to a cartesian system but as a geojson type dict:

shape = {"type": "FeatureCollection"}
shape['features'] = [{ "type": "Feature",
                                     "geometry": {"type": "Polygon",
                                      "coordinates":  array_type }}]

where array_type is a list of lists of the form:

[
          [
            [lon_1, lat_1], 
            [lon_2, lat_2],
            ...
           [lon_n, lat_n]
          ]
        ]
    

and (lon_k, lat_k) with k=1:n are geographical coordinates of the points on a circle with center (lon_c, lat_c).
For details on the geojson type dict see https://en.wikipedia.org/wiki/GeoJSON

layers definition is inserted within layout.mapbox:

fig.update_mapboxes(layers=layers)