@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))