TODAY learn how to bring Drag & Drop to Dash with Dashboard Engine. 🌠 Sign up for the live event!

How to show both regions and points on a map?

Hello,

So, this is my situation.

  1. Using various variables, I had grouped around 800 counties into different clusters. I have color coded them according to the cluster number (1, 2 and 3). For this I have used plotly.express. choropleth.

  2. A list of geocoded businesses that I have plotted using plotly.express. scatter_mapbox.

What can I use to overlap the two? I mean I would like the color coded counties and the businesses to appear on the same map. Is it possible to use plotly or do I have to use something else?

I’m not sure about “px”, but I use “go” to add multiple layers. In the example below, I add a route line, stop locations, and a vehicle location to the same map.

vehicle_fig = go.Figure()

        vehicle_fig.add_trace(go.Scattermapbox(
            name='Route',
            lat=route_lat,
            lon=route_lon,
            mode='lines',
            hoverinfo='none',
            opacity=1,
            line=go.scattermapbox.Line(
                width=8,
                color='#669DF6'
            ),
            showlegend=False
        ))

        vehicle_fig.add_trace(go.Scattermapbox(
            name='Stop',
            lat=stop_lat,
            lon=stop_lon,
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=36,
                color='#636363'
            ),
            showlegend=False
        ))

        vehicle_fig.add_trace(go.Scattermapbox(
            name='Vehicle',
            lat=lat,
            lon=lon,
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=36,
                color='#636363'
            ),
            showlegend=False
        ))

        vehicle_fig.update_layout(
            autosize=True,
            hovermode='closest',
            margin=dict(l=0, r=0, t=0, b=0),
            geo=dict(resolution=50),
            mapbox=go.layout.Mapbox(
                accesstoken=mapbox_access_token,
                style=map_layer,
                bearing=0,
                center=go.layout.mapbox.Center(
                    lat=(max(all_lat) + min(all_lat)) / 2,
                    lon=(max(all_lon) + min(all_lon)) / 2,
                ),
                pitch=0,
                zoom=calc_zoom(min(all_lat), max(all_lat), min(all_lon), max(all_lon))
            ),
            dragmode=False
        )

Thanks for your idea. I used choropleth and scattermapbox to show my points on top of my shaded regions.

fig = go.Figure()

for i, cluster in enumerate(df['Cluster'].unique()):
    dfp = df[df['Cluster'] == cluster]
    fig.add_choroplethmapbox(geojson=counties, 
                             locations=dfp['FIPS'],
                             z=[i,] * len(dfp), 
                             showlegend=True, 
                             name=cluster,
                             colorscale=colorscales[i], 
                             showscale=False)

fig.add_trace(go.Scattermapbox(
        lat=site_lat,
        lon=site_lon,
        mode='markers',
        marker=go.scattermapbox.Marker(
            size=5,
            color='rgb(0, 128, 0)',
            opacity=0.3
        ),
        text=locations_name,
        hoverinfo='text'
    ))

fig.update_layout(geo_scope='usa',
                  mapbox=dict(style="carto-positron", zoom=3,center={"lat": 37.0902, "lon": -95.7129},
                              ))

fig.show()