How to create multiple colorbars in a scattermapbox

Hi, I have a dataframe that has lat, long, speed, and mode. Mode is a categorical data and Iā€™d like to plot different colorbars based on the mode.

Below is what Iā€™d like to end up plotting: each mode having its own colorbar, and the data is colored by speed.

Using this code, Iā€™m able to get it to work when dealing with one drive mode only,

fig = px.scatter_mapbox(df[df[ā€˜drive_modeā€™] == ā€˜mode1ā€™], lat=ā€œLatā€, lon=ā€œLonā€, color=ā€˜VHW_Speedā€™,
labels = {ā€˜VHW_Speedā€™: ā€˜mode1 Speedā€™})

how do I add a second set of the dataframe with its own unique colorbar? When I try to assign a second subsection of the dataframe using:

fig1 = (df[df[ā€˜modeā€™] == ā€˜mode2ā€™] yada yada)
then:
fig.add_trace(fig1)

the datapoints from the second fig shows up, but I still only get the colorbar from the first fig.

Apologies if Iā€™m using the incorrect terminologies. initially I was searching up terms like ā€œmultiple legends with range of valuesā€ and I think one post online said it was called a colorbarā€¦

2 Likes

Does anyone know if thereā€™s a way to do this in the Python Plotly implementation, perhaps with separate overlaid data for each mode? Not that it helps here, but Iā€™m also curious if in the Plotly JavaScript language itā€™s possible to get multiple different colorbar legends to display for a single trace?

Iā€™m following your question @RSDummy.

Hotfix update:

I managed to find a way to generate multiple colorbars; however, it is hard coded - the length and position of the colorbars are fixed.

looks a little something like this,

fig0 = px.scatter_mapbox(dff, lat=ā€œLatā€, lon=ā€œLonā€)
fig0.data[0].marker = dict(
colorscale=ā€˜bluesā€™,
colorbar = dict(x=1,y=.9, len = 0.25),
color = ā€˜Speedā€™,)
fig.add_trace(fig0.data[0])

and repeat for successive fig1, fig2 etc, the positioning and length of the colorbar is controlled using the x,y, and len respectively.

I honestly do not understand how the numbers work for the x and y work. Based on the documentation in link they both range from [-2,3]; however, I donā€™t understand what each value represents (0<x<1 make the most sense to me, y works very differentlyā€¦) , and resorted to hard coding the numbers myself.

I wanted to implement this multi-colorbar inside a loop, but I couldnā€™t come up with a formula that would adjust the x and y position of the successive colorbars dynamically.

Oh well, another day of terrible code I guess.

image to show what I ended up with, unfortunately when I disable specific data corresponding to a colorbar, the hard code reveals its nastiness

hard coded update #2ā€¦

by manually figuring out the y, Iā€™ve managed to implement a dynamic colorbar length, terrible code for those who just wants something that worksā€¦

count = 0
len_offset = { 1:0.375, 2:0.125, 3:0.125/3, 4:0 }
manual_colorscales = {1:ā€˜bluesā€™, 2:ā€˜redsā€™, 3:ā€˜electricā€™, 4:ā€˜greensā€™}
for i, txt in enumerate([ā€˜SGT XCONā€™, ā€˜PGT XCONā€™, ā€˜SHAFT STPā€™, ā€˜TWO XCONā€™]):
n = len(checklist)
if txt in checklist:
figx = px.scatter_mapbox(dff[dff[ā€˜drive_modeā€™] == txt], lat=ā€œLatā€, lon=ā€œLonā€)
figx.data[0].marker = dict(
colorscale=manual_colorscales[i+1],
colorbar = dict(x=1,y= 0.875 - (count/n) - len_offset[n] , len = 1/n, title = txt),
color = dff[dff[ā€˜drive_modeā€™] == txt][plot_val])
count+=1
fig.add_trace(figx.data[0])

fig.update_layout(mapbox_style= ā€˜lightā€™, mapbox_accesstoken=token, margin=dict(l=0,r=0,b=0,t=0))