How to specify color legend labels in mapbox plot?

Maybe this us a beginner’s question…
I am trying to plot a mapbox scatter of timestamped values. To get a nice continuous color range, I map the timestamps to integers. However, in the end I want to see the original timestamps again in the color scale legend, not the integers. How do I do that?

Picture attached. The labels below “color” should display the timestamps.

df_nonan = df.dropna(subset=['Counts_DC_#cm3'])
date_to_val = df_nonan['DateTime'].map(pd.Series(data=np.arange(len(df_nonan)), index=df_nonan['DateTime'].values).to_dict())

fig_map = go.Figure()
fig_map = px.scatter_mapbox(df_nonan, lat="Lat", lon="Lon", color=date_to_val, size="Counts_DC_#cm3", 
                            color_continuous_scale=px.colors.sequential.Inferno, size_max=25, zoom=15, height=800,
                            )
fig_map.update_traces(
     patch=dict(mode='text+markers', text=df_nonan['DateTime'], marker_opacity=0.2),
     selector=dict(type='scattermapbox'))
fig_map.show()

Hi @peterho!
I have never had the occasion to use px.scatter_mapbox, but you should be able to map the coloraxis tick labels to anything you want, like:

import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="size",
                 title="Numeric 'size' values mean continuous color")
fig.update_coloraxes(
    colorbar_tickvals=[1, 2, 3, 4, 5, 6],
    colorbar_ticktext=['One', 'Two', 'Three', 'Four', 'Five', 'Six'],
)

image image

1 Like

Here we go, to find the lists for tickvals/ticktext, try:

n_ticks = 6
step = (date_to_val.max() - date_to_val.min()) / (n_ticks-1)
tickvals = [date_to_val.min() + step * i for i in range(n_ticks)]

df['DateTime'] = pd.to_datetime(df['DateTime'])
step = (df['DateTime'].max() - df['DateTime'].min()) / (n_ticks-1)
ticktext = [df['DateTime'].min() + step * i for i in range(n_ticks)]

fig.update_coloraxes(
    colorbar_tickvals=tickvals,
    colorbar_ticktext=ticktext,
)