Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Grouped traces on maps with lines


I’m using Plotly to generate a map with a lines in three different colours:

I’d like to be able to use the legend to toggle each of the groups on and off, but when I add a legend, it shows all the individual traces, so I’d have to click dozens of times to achieve the desired effect.

Is there a way to group these traces so that the legend will just show (e.g.) “blue”, “green”, and “orange”, so that I can toggle them all at once?

Also, a separate question: is it possible to specify different size nodes (markers) at each end of an edge (line)? So far I have only been able to set one size.

Thanks in advance! :slight_smile:

Try using legendgroup:

1 Like

You, sir, are a legend (pun intended).

Thank you very much indeed! :grinning:

@etienne So I have just got round to giving this a try, but it doesn’t seem to work:

Here’s the code:

Any idea what I’m doing wrong?

Thanks in advance!

Oh. You’ll need to add showlegend: false to all the traces you don’t want to show in the legend. In your case that would all but the first trace of each legendgroup value.

Ah, got it. So I think I’m almost there…

Now the grouping works (woohoo!), but the traces are all still visible in the legend. It could be my logic, though… would you mind double-checking to see what I’ve done wrong? :slight_smile:

Hey @etienne, any idea what I’m doing wrong here?

Fixed it. Problem between computer and chair.

Incidentally, setting traceorder: 'reversed+grouped' doesn’t work (despite, although traceorder: 'reversed' works fine.

Thanks again for all your help! :slight_smile: