✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Displaying customized geojson with both 'Polygon' and 'Point' features using plotly.express

Hi,

I’m using plotly.express to display a customized (built from scratch) geojson file with several ‘Polygon’ features.
The customized geojson has the usual structure for the features,
‘geometry’: {‘type’: ‘Polygon’, ‘coordinates’: [coords]}}),
and everything works just fine with plotly.express.choropleth() and all the features show up as expected.

However, if I try to add points to the geojson as in
‘geometry’: {‘type’: ‘Point’, ‘coordinates’: [coords]}}),
they do not show up.

I know that there is a method plotly.express.scatter_geo(), but I assume this would only serve to plot the points.

So the question is: How do I mix customized geojson ‘Polygon’ and ‘Point’ features in a single figure? Do I have to “make” small polygons (circles, squares, or triangles) just to show the points? I could do that, but it seems very inefficient.

Thank you.

You could try Dash Leaflet. It supports both Polygons and Points. Here is an example,

You can also add arbitrary GeoJSON layers & features to plotly maps. See the GeoJSON Layers example here: https://plotly.com/python/filled-area-on-mapbox/#geojson-layers. Note that they won’t have the same nice hover features as the data-driven options.

Thank you so much. Although I have worked with leaflet in R and got good results, I really don’t want to go that direction this time. I’m looking for something that feels more ‘plotlyonic’ this time.

Thank you for the suggestion. I’ve tried the sample code on a triangle, inside the callback, just to see what I would get, before attempting with ‘Points’. The polygons in the geojson show up as previously and it doesn’t throw any error, but the test polygon doesn’t show up.

fig1 = px.choropleth(new_descriptor, geojson=new_map1, featureidkey='properties.NAME',
                     locations='NAME', projection='mercator', hover_data=['NAME'],
                     color=tipo_gestor,
                     color_discrete_sequence=['rgba(220,220,220,0.2)',
                                              'rgba(250,165,47,0.6)', 'rgba(62,129,19,0.6)',
                                              'rgba(250,165,47,0.8)', 'rgba(62,129,19,0.8)'])
fig1.update_traces(dict(showscale=False, coloraxis=None))
fig1.update_geos(fitbounds='locations', visible=False)  # don't print world map
fig1.update_layout(
    autosize=True, showlegend=False,
    mapbox={
        'layers': [{
            'source': {
                'type': "FeatureCollection",
                'features': [{
                    'type': "Feature",
                    'geometry': {
                        'type': "Polygon",
                        'coordinates': [[[-9.0, 39.5], [-8.5, 39.5], [-8.7,39.0]]]
                    }
                }]
            },
            'type': "fill", 'below': "traces", 'color': "royalblue"}
        ]},
    margin={"r": 0, "t": 0, "l": 0, "b": 0}
)
return fig1