✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

How can I combine Choropleth and Scatter layer in a plotly map?

Hello!

I’m trying to switch from RShiny to Dash and I have some trouble with recreating pretty basic map stuff in Plotly.
I want to make scattermap on top of a layer of custom choropleths (neighbourhoods in NL). These choropleths have a value that is the sum of an attribute of the points in a scatter layer.

Is there an option, like there is for instance in leaflet, to make a scatterplot layer and a custom choropleth layer from a panda’s geodataframe and overlay them on a basemap? See the example below, made with R and Leaflet:

I’ve seen some examples of adding rasters in the layers component of the layout, but I haven’t seen a working example with choropleth and scatterplot layers and it seems like you don’t have as much control over the second layer if you do it this way.

Thanks in advance!

Something of the following form should work

choropleth_data = {
     # Your choropleth data here
}

scatter_data = {
     # Your scatter data here
}

figure = {
    "data": [
        choropleth_data,
        scatter_data
    ]
   "layout": {
        # Your layout dictionary here
   }
}

Thank you all so much! This worked :slight_smile:

Hello. Having a lot of difficulty with this as well. Surprised there’s not a lot I can find on Google. Currently the Jupyter link doesn’t work (@empet). Also, I’m not really sure how to use @sjtrny’s example using R.

@joeflack4

To define a go.Figure() that contains both a chroplethmapbox and a scattermapbox, proceed as follows:

fig = go.Figure(go.Chroplethmapbox()) # here you set all attributes needed for a Choroplethmapbox
fig.add_scattermapbox(lat = your_list_of_latitudes,
                      lon = your_list_of_longitudes,
                      mode = 'markers+text',
                      text = some_text,  #a list of strings, one  for each geographical position  (lon, lat)              
                      below='',                 
                      marke_size=12, marker_color='rgb(235, 0, 100)'))
fig.update_layout((title_text ='Your plot title', title_x =0.5, 
                   mapbox = dict(center= dict(lat=52.370216, lon=4.895168),  #change to the center of your map          
                                 accesstoken= "your-mapbox-access-token",
                                 zoom=6, #change this value correspondingly, for your map
                                 style="light"  # set your prefered mapbox style
                               ))