Add legend 'auto' x|y and/or 'container' (x|y)ref
The problem is that the horizontal legend overlaps the x-axis title. It occurs if the legend is positioned below (rather than to the right of) the plot area using layout = {legend: {"orientation": "h"}};. It aggravates in combination with multiline x-axis titles (using <br> tags).
Currently there's no way to control the spacing between the legend and the plotting area. Everything is auto-scaled, it is useless to set the bottom margin (layout = {margin: {"b": ...}};) in this case because it refers to the space below the legend. The only way to make the whole x-axis title visible is to increase the height of the whole plot until the autoscaling sufficiently spreads the legend and the plotting area apart.
I try to illustrate the problem with the following 3 images which differ only in frame height:
height 700px:

height 1000px:

height 1300px:

Here is the above plot hosted on plot.ly.
+1
+1
Although I appreciate the great demand for a fix of this issue, I doubt that all the "+1" comments (and the thereby triggered e-mail notifications) will eventually help to motivate the plotly developers to finally implement a solution for the problem. So just use GitHub reactions to express your support, please 😉
Any updates regarding above issue?
Digging into this a little in relation to #2243 - the background:
- Legends are positioned in paper-referenced coordinates - those are the coordinates where the bottom left of the plotting area (excluding the margins in which we draw axis labels etc) is (0,0)
- The default position is
x=0, y=-0.1, xanchor='left', yanchor='top'- so the left edge aligned with the left edge of the plotting area, and the top in the margin, one tenth of the y axis length below the plotting area, on the theory that axis labels typically fit within that gap - but of course that's not always true! - So if you know the size of the plot AND the area needed for axis labels and title, you can choose
legend.ysuch that there will be no overlap. - But if you need this to work with dynamic plot size or axis label length, the auto-margin mechanism we have (which is used right now to increase the margin in order to keep the legend from extending outside the visible area and will soon be used to ensure long axis labels are always visible, see #2243), CANNOT be used as is to keep the legend from overlapping axis labels.
I see two possible ways to go here. The first is less of an addition to what we currently have, but the second I think would be more powerful and probably easier to implement too:
- Have the auto-margin system automatically change
legend.y. Whenlegend.yis not explicitly set, it would have to have an'auto'value or something, then the auto-margin system would have to figure out how to combine space needed for the axis labels with space needed for the legend. But this is still problematic: what if you want it at the top and you have a top x axis whose labels it needs to avoid? Would we need'auto-top'and'auto-bottom'? That sounds awkward, and there may be other possibilities I'm not thinking of. - Alternatively, allow the legend to be positioned in other coordinates - I'm thinking of the
'container'coordinates as proposed in #882 for chart titles, in which (0,0) is the absolute bottom left corner including margins. Then we could default a horizontal legend tox=0, xanchor='left', xref='paper'(that wouldn't change) andy=0, yanchor='bottom', yref='container'(that part is new, though the result would be about the same as what we have now. In addition we could addpadattributes as also suggested in #882, which would add a pixel buffer between the legend and the edge of the div, and between the legend and the plot or axis labels) - and at that point, the auto-margin system could simply reserve space for anything container-referenced (including the title 🎉 ) before fitting the other things in.
@nicolaskruchten @etpinard
Any chance this is being worked on? Would be pretty clutch for my project.
Was there any work being done on this or perhaps another issue that led to a fix?
Another example from https://github.com/plotly/plotly.js/issues/4261
When there are a large number of traces in a horizontal legend, they begin to overlap to x-axis label as the legend grows in size. It would be great if the horizontal legend added a scrollbar if there are >3-4 rows of legend items, or if this could be configurable.
This seems somewhat related to this issue: https://github.com/plotly/plotly.js/issues/960

Any word on a fix for this?
We are chipping away at various pieces of this problem and related problems... no commitment on timelines at this time :)
My feeling is that with *axis.automargin = true, the margin pushing code should take into account the legend in cases when it's in horizontal mode below the plot, leaving room for legend plus the axis/tick labelling, rather than taking the max as we currently do, thereby allowing them to overlap. I think we should resolve #4222 the same way. This would allow us to not add extra attributes or auto-layout modes.
Maybe we could do this only in cases where the legend positioning is outside of [0-1] or something, so that people could still place legends within the plotting area on purpose?
the margin pushing code should take into account the legend in cases when it's in horizontal mode below the plot, leaving room for legend plus the axis/tick labelling,
Leaving room for both the tick labels and the legend in the margin wouldn't be too hard, but the hard part here (which we might need a new attribute for) is to reposition the legend below the ticks. The new legend position will depend on the tick label dimensions and thus we would have to introduce some new cross-component logic.
The new legend position will depend on the tick label dimensions
Is the mechanism here because longer labels means pushing the margins up means in effect moving the (0,0) coordinate that the paper/domain coordinate space works within?
I think I'm understanding the problem and suggested solutions better now... the container ref idea sounds like a pretty good one as a low-level control system. We may want to layer on top of that a "placement" enum that would be the 4 corners and 4 faces of the rectangle, which would automatically set anchors and refs to container etc so that orientation="h" with placement="bottom center" would do the right thing and play well with automargins?
Moved to 1.53.0
Is there any update on this? I have some legends with a lot of text, the text is dynamic, so I can't do much about hose long the text is. When the chart generates, the legend overlaps the x-axis legend considerably.
Is there anything the community can do to help?
If someone wants to take on some implementation work, we'd be happy to iterate on a design and review some PRs, but at the moment this is no longer on our short-term roadmap.
If you work for an organization with a software budget, you could sponsor this feature to bump it up our priority list.
- I didn't know sponsoring a feature could be a thing
- I'll try to take a look to help out
Great! :)
This issue has been tagged with NEEDS SPON$OR
A community PR for this feature would certainly be welcome, but our experience is deeper features like this are difficult to complete without the Plotly maintainers leading the effort.
Sponsorship range: $10k-$15k
What Sponsorship includes:
- Completion of this feature to the Sponsor's satisfaction, in a manner coherent with the rest of the Plotly.js library and API
- Tests for this feature
- Long-term support (continued support of this feature in the latest version of Plotly.js)
- Documentation at plotly.com/javascript
- Possibility of integrating this feature with Plotly Graphing Libraries (Python, R, F#, Julia, MATLAB, etc)
- Possibility of integrating this feature with Dash
- Feature announcement on community.plotly.com with shout out to Sponsor (or can remain anonymous)
- Gratification of advancing the world's most downloaded, interactive scientific graphing libraries (>50M downloads across supported languages)
Please include the link to this issue when contacting us to discuss.