Search code examples
pythonplotlymapbox-marker

Is there a way to always show all markers in a plotly scattermapbox, regardless of manual zooming?


I am trying to generate several maps with different content based on a dataframe. So far, I have managed to display the information I needed on the interactive maps. However, as I need to include the generated maps as figures in a report, I need to find a way to show all the markers in the figures. Problem is: some markers only are shown when I manually zoom in the area. Is there a way to always make the markers visible?

Here is the code:

import plotly.graph_objects as go

token = open("token.mapbox_token").read() # you need your own token

df_select = df_map.loc[df_map['Budget'] == 0.9]

fig= go.Figure(go.Scattermapbox(lat=df_select.Latitude, lon=df_select.Longitude,
                       mode='markers', marker=go.scattermapbox.Marker(
                          size=df_select.Warehouse_Size*5, color = df_select.Warehouse_Size, 
                          colorscale = ['white','red','orange','green','blue','purple'], 
                          showscale = False)))

fig = fig.add_trace(go.Choroplethmapbox(geojson=br_geo, locations=df_select.State, 
                           featureidkey="properties.UF_05", 
                           z=df_select.Top10,
                           colorscale=["white","pink"], showscale=False,
                           zmin = 0,
                           zmax=1,
                        marker_opacity=0.5, marker_line_width=1
                          ))

df_prio = df_select.loc[df_select['Prioritisated'] == 1]

fig= fig.add_trace(go.Scattermapbox(lat=df_prio.Latitude, lon=df_prio.Longitude+1, 
                       mode='markers', 
                       marker=go.scattermapbox.Marker(symbol = "campsite", size = 10)))

fig.update_layout(height=850,width = 870,
                  mapbox_style = "mapbox://styles/rafaelaveloli/ckollp2dg21dd19pmgm3vyebu",  
                  mapbox_zoom=3.4, mapbox_center = {"lat": -14.5 ,"lon": -52}, 
                  mapbox_accesstoken = token, showlegend= False)

fig.show()

This is the result I get:

enter image description here

And this is one of the hidden markers that are only visible when zooming in: enter image description here

How can I make it visible in the first figure, without changing the figure zoom and dimensions?


Solution

  • Passing allowoverlap=True to go.scattermapbox.Marker() seems to resolve the issue (link to relevant docs).