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