✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Possible to have nested (or hierarchical) legends?

This is what I would think “legend groups” are for, but by the examples, does not seem to do what I want. I would like to be able to have something like this for plots that say have 4 traces, with two in one category and two in another. The categories would be the same color.

Women
—*--- Age 10-30
—±-- Age 30-60
Men
—o--- Age 10-30
#--- AGe 30-60

2 Likes

Hi there,
Nested legends aren’t possible at the moment.
Legend groups currently available in Plotly are for grouping traces so you can toggle groups of traces on/off at once (https://plot.ly/python/reference/#scatter-legendgroup).

Thanks Chelsea! I was trying to understand the value of legend groups–at first glance seemed to be just another way to set line style. But the toggle thing is really nice.

1 Like

Hi, I am looking for the same feature and I can’t find any update for this. I found that grouped axis label has been developed (https://github.com/plotly/plotly.js/issues/2799) so now I am wondering if grouped legend is available too but I can’t find anything on the official website (https://plot.ly/python/legend/)

@maxpuff it is under Grouped Legend here https://plot.ly/python/legend/

Thanks but I meant in a way it is grouped under a category. The grouped legend that is in the doc groups legend so that if you click on the group it displays all/none of the legend in this group, however we can’t add one trace from a group.
If we take the example from the original post, what I am looking for is a group that enables the user to select the whole group or part of this group:
(I am adding the check box to understand the behavior but it does not have to be displayed)

If we click on Men, it removes all the legends under the men group (this is currently working like this, with a different display: either there is no label ‘men’ or we can define one label only for the group)

:ballot_box_with_check:Women
_☑ —*— Age 10-30
_☑ —±-- Age 30-60
☐Men
_☐ —o— Age 10-30
_☐ —#— AGe 30-60

Having a nested legend could enable the user to :

  • add to/remove from graph a whole group containing several traces
  • add/remove a single element from the group

So we could display:

:ballot_box_with_check:Women
_☑ —*— Age 10-30
_☑ —±-- Age 30-60
☐Men
_☐ —o— Age 10-30
_☑ —#— AGe 30-60