How to add Marker to Density Mapbox?

I want to put some markers on the Density Mapbox. I tried below, but no luck.
Can someone help me?

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')


import plotly.express as px
import plotly.graph_objects as go

fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
                        center=dict(lat=0, lon=180), zoom=0,
                        mapbox_style="stamen-terrain")

fig.add_trace(go.Scattergeo(
    lat=[
        38.897957,  # White House   
        48.858093,  # Eiffel Tower
    ],
    lon=[
        -77.036560,
        2.294694,
    ],
    mode='markers',
    marker= {'size': 20, 'symbol': ["diamond"]},
))

fig.show()

Hi @tsuga324

You are not able to combine Plotly express plots and graph objects together. Graph objects would provide flexibility over designing and hence would be best when you want to do more complex things. Plotly express allow you to do some quick plots in an aesthetic fashion.

I have placed an example here using the same code above but I am leaving the symbol to be the default one which is the circle.

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

raw_symbols = SymbolValidator().values

fig = go.Figure(
            go.Densitymapbox(
            lat=df["Latitude"],
            lon = df['Longitude'],
            z = [10],
            radius = 10,
))

fig.update_layout(mapbox_style="stamen-terrain",height = 700,
                  mapbox = {
                          'center': {'lat': 0, 
                          'lon': 180}
                  })

fig.add_trace(go.Scattermapbox(
        lat=[38.897957,48.858093],
        lon=[-77.036560,2.294694],
        mode='markers',
        marker={'size': 50 },
    
    ))




fig.show()

For a custom symbol it is not that easy and I think you need a mapbox token to have it working.

  • symbol
    Code: fig.update_traces(marker_symbol=<VALUE>, selector=dict(type='scattermapbox'))
    Type: string or array of strings
    Default: "circle" Sets the marker symbol. Full list: https://www.mapbox.com/maki-icons/ Note that the array marker.color and marker.size are only available for β€œcircle” symbols.

You can check out this page as well where there is an inbuilt package for symbols. Styling Markers | Python | Plotly

Do experiment them and improve the answer.

Let’s see if someone else can help with modification of this code.

Thank you! It works for me!

1 Like