Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Color not shown in Legend

In Plotly Graph Objs, I use a column in the dataframe to color the markers.

fig = go.Figure(go.Scattermapbox(
lon = df[‘LON’],
lat = df[‘LAT’],
name = ‘Point of Interest’,
mode = ‘markers’,
marker = go.scattermapbox.Marker(
size = 5,
color = df[‘Colour’]
)
,
),
).update_layout(
mapbox = {
‘accesstoken’ : mapbox_access_token,
},
showlegend = True
)

fig.show()

What I end up in the legend is just a single dot with the name of the trace "Point of Interest’.

How can I create a legend with the colors that correspond to those I used (i.e. df[‘Colour’])?

Thanks

@marlin9107

If your df['Colour'] is a numerical column, then by your code each value is mapped to a colorscale. Because you did not give a marker_colorscale=somecolorscale, plotly uses its default colorscale, Plasma. In this case (i.e. when you are mapping the scalar values to a colorscale)
the legend doesn’t display marker colors, but instead you can attach a colorbar:
marker_showscale=True.

If you want to map some values to a specific color, depending on some class or category, then this example can help:

lats = [45.5017, 43.761539, 45.411171, 46.829853,  46.498390]
lons = [-73.5673, -79.411079, -75.6981201, -71.2540, -66.159668] 
color = [ 12.35, 21, 17.9, 24, 15]
group = ['A', 'B', 'A', 'C', 'B']
d = dict(lat =lats, lon=lons, color=color, group =group)
df = pd.DataFrame(d)

gb = df.groupby(['group'])
group_name = list(set(df['group']))
fig = go.Figure()
for gr in group_name:
    my_gr = gb.get_group(gr)
    
    fig.add_trace(go.Scattermapbox(lon=my_gr['lon'],
                                   lat=my_gr['lat'],                  
                                   customdata = my_gr['color'],
                                   hovertemplate = '<b>Value</b>: %{customdata}',                   
                                   name=gr,                                    
                                   mode='markers',
                                   marker=dict(size=14),
                                    ))

fig.update_layout(width =800, height=600,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center_lat =45, center_lon=-75
        ,
        pitch=0,
        zoom=4,
        style='basic'
    ))
fig.update_layout(legend = dict(bordercolor='rgb(100,100,100)',
                                borderwidth=2,
                                itemclick= 'toggleothers',
                                x=0.91,
                                y=1))

@empet

Thanks for the explanation. It makes sense.

I’ve one related question. I add another trace using a custom symbol (‘triangle’) instead of the default ‘circle’. Both the existing and new traces are shown on the map. I googled on the topic of using different symbol and understand that not every symbol in this official link works.

https://labs.mapbox.com/maki-icons/

My question is about the legends. Now that I have 2 traces. The legend for the first one works perfectly with your suggestion. There is one problem with the 2nd legend.

  1. it’s showing a dot/circle instead of a triangle. How can I change that to a triangle?
fig.add_trace(go.Scattermapbox(
                                name='Route',                           
                                lat=cells['LAT'], 
                                lon=cells['LON'],  
                                mode='markers',
                                marker={
                                    'size':5,
                                    'symbol':'triangle'
                                    },                          
                                legendgroup=1
                    
                    )
        )

Would be appreciated if you could point me to the right direction.

@marlin9107

Unfortunately Scattermapbox doesn’t work yet with other marker symbols than the default one (circle). Download this Jupyter notebook https://chart-studio.plotly.com/~empet/15877/bug-in-scattermapbox/#/ and run successively its cells to see the bug in symbol colors and their legend representation.

@empet

Many thanks for the tests to prove that there is a bug with Scattermapbox.

I’ll just use ‘start’ and stick with one single colour in the map for the 2nd trace and the star is shown in legend.