I am struggling to get the labels for categories to show up on my manually created legend from a Scatter.Mapbox trace with a colorbar. If you see the image below, you will notice that the bar is rendering correctly, but that is not rendering the labels for some reason.
Here is my code:
color_vals = ["#DAE3F3","#8FAADC","#4472C4","#ED7D31","#F4B183","#FBE5D6","#FF0000"]
color_names = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7']
num_colors = len(color_vals)
colorscale=[
[0, "#DAE3F3"], # Cat1
[0.14286, "#DAE3F3"],
[0.14286, "#8FAADC"], # Cat2
[0.28571, "#8FAADC"],
[0.28571, "#4472C4"], # Cat3
[0.42857, "#4472C4"],
[0.42857, "#ED7D31"], # Cat4
[0.57143, "#ED7D31"],
[0.57143, "#F4B183"], # Cat5
[0.71429, "#F4B183"],
[0.71429, "#FBE5D6"], # Cat6
[0.85714, "#FBE5D6"],
[0.85714, "#FF0000"], # Cat7
[1, "#FF0000"]]
cmin = .5
cmax = num_colors + cmin
category_colors = {
'cat1': "#DAE3F3",
'cat2: "#8FAADC",
'cat3': "#4472C4",
'cat4: "#ED7D31",
'cat5': "#F4B183",
'cat6': "#FBE5D6",
'cat7: "#FF0000",
}
cvs = ds.Canvas(plot_width=4000, plot_height=4000)
agg2 = cvs.polygons(bg_stats3, geometry='geometry', agg=ds.by(selected_variable, ds.any()))
agg = cvs.polygons(bg_stats2, geometry='geometry', agg=ds.by(selected_variable, ds.any()))
img = tf.shade(agg2, color_key=category_colors, how='eq_hist')[::-1].to_pil()
coords_lat, coords_lon = agg.coords["y"].values, agg.coords["x"].values
coordinates = [
[coords_lon[0], coords_lat[0]],
[coords_lon[-1], coords_lat[0]],
[coords_lon[-1], coords_lat[-1]],
[coords_lon[0], coords_lat[-1]],
]
fig = go.Figure(go.Scattermapbox())
fig.update_layout(
mapbox_style='carto-positron',
mapbox_layers=[
{
"sourcetype": "image",
"source": img,
"coordinates": coordinates
}
]
)
color_scale_legend = go.Scattermapbox(
lat = [0],
lon = [0],
mode='markers',
marker=dict(
size=0,
color=[cmin, cmax],
colorscale=colorscale,
showscale=True,
colorbar=dict(
title=selected_variable,
x=0.95,
y=0.5,
len=0.6,
tickmode='array',
tickvals=color_vals,
ticktext=color_names,
thickness=20,
tickcolor='black',
tickfont=dict(color='black')
)
)
)
fig.add_trace(color_scale_legend)