plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Add legend 'auto' x|y and/or 'container' (x|y)ref

Open salim-b opened this issue 10 years ago • 48 comments

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: example_height700

height 1000px: example_height1000

height 1300px: example_height1300

Here is the above plot hosted on plot.ly.

salim-b avatar Nov 25 '16 17:11 salim-b

This is happening because layout.margin.b is set to 0 and the default position for horizontal legends is:

x: 0,
xanchor: 'left',
y: 1.1,
yanchor: 'bottom'

@etpinard Would it make sense to make the defaults smarter and check the value of layout.margin.b?

n-riesco avatar Nov 25 '16 18:11 n-riesco

... and check the value of layout.margin.b?

I don't think layout.margin.b is central to this. If you play around a little with different combinations of layout.margin.b and layout.legend.yanchor, you quickly see that in the plot above you'll get nowhere desirable... actually the default of layout.legend.yanchor, which seems to be top not bottom (at least when I'm plotting in R), leads to the best results.

The problem in my eyes is that – when layout.legend.orientation is horizontal and the rest of the legend positioning parameters left to their default values – the legend overlaps the plotting area in the first place. In the default case the legend should always be placed below the x-axis, then it would be fine.

salim-b avatar Nov 25 '16 23:11 salim-b

Probably a duplicate of https://github.com/plotly/plotly.js/issues/1190

etpinard avatar Nov 28 '16 15:11 etpinard

I was having a similar issue with the horizontal legend overlapping x axis values, when the x axis values were long enough, like dates. I'm adding this here because it's the only issue that comes up when you google it, and it seems related. This happens with only setting {legend: 'horizontal'} in the layout and nothing else.

legend override

I tried a couple of different things, like playing with layout.legend.yanchor and the margins, but the legend doesn't appear to be affected by margins, it stays in the same place relative to the dates, and yanchor shifts it a little bit but it remains on top of the x axis ticks.

In the end my solution was to absolutely position the legend using legend: { "orientation": "h", xanchor: "center", y: 1.2, x: 0.5 },

This has the side effect of putting the legend on top of the graph, but you can position it below the graph by lowering the Y value. It appears that the default X and Y values according to the docs (1, 1.2) are changed to a different value when the legend is set to horizontal, which makes sense, but should also be reflected in the documentation here:

https://plot.ly/javascript/reference/#layout-legend

I chose to put it above the graph because I know my x axis values are going to change in length and the graph is being iframed into a small spot, so I don't want to waste space. Not super pretty or anything, but a reasonable workaround.

legend override 2

Hopefully the horizontal legends are changed so that it will take into account x axis tick length automatically, or there's another easy way to do it I haven't found.

herrgruebner avatar Jan 24 '17 19:01 herrgruebner

I am running into this issue also.

ck37 avatar Mar 02 '17 20:03 ck37

Same Issue :(

jinuem avatar Mar 06 '17 07:03 jinuem

+1

hy9be avatar Mar 06 '17 18:03 hy9be

+1

suribabu39 avatar Apr 11 '17 10:04 suribabu39

+1

anafziger avatar Apr 17 '17 13:04 anafziger

+1

kiezuz avatar Apr 19 '17 08:04 kiezuz

+1

FrantisekGazo avatar May 01 '17 06:05 FrantisekGazo

+1

yunxileo avatar May 03 '17 08:05 yunxileo

+1

allanelder avatar May 22 '17 20:05 allanelder

+1

tabuechn avatar Jun 16 '17 13:06 tabuechn

Did they fix this issue? Does Plotly takes care of it without us having to hack around? If so how can we position the legend at bottom of chart without overlapping with x-axis?

ghost avatar Jul 18 '17 19:07 ghost

Did they fix this issue?

Nope, I don't think so. At least when I'm plotting in R (newest CRAN version based on plotly.js 1.27.1) the issue is still the same. Also I don't think it has been fixed in newest plotly.js 1.28.3 since I can't find anything related in the changelogs.

salim-b avatar Jul 19 '17 10:07 salim-b

+1

nzjrs avatar Aug 08 '17 08:08 nzjrs

Just to report, plotly version 4.7.1, using R 3.4.1 and embedding the graph in a RNoteBook, as today, still same problem. Looking for a workaround now. Some of the examples above seem useful, will try.

convergentist avatar Aug 26 '17 15:08 convergentist

None of the work arounds worked for me, it would be helpful if you report success here if you have any

On 26 Aug 2017 17:47, "Francisco Muñoz" [email protected] wrote:

Just to report, plotly version 4.7.1, using R 3.4.1 and embedding the graph in a RNoteBook, as today, still same problem. Looking for a workaround now. Some of the examples above seem useful, will try.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/plotly/plotly.js/issues/1199#issuecomment-325139960, or mute the thread https://github.com/notifications/unsubscribe-auth/AACjo5yRTlVcR4JUbUCIB_e8u6SXvcfcks5scD4KgaJpZM4K8oWX .

nzjrs avatar Aug 26 '17 16:08 nzjrs

+1

vladimirlukyanov avatar Oct 12 '17 11:10 vladimirlukyanov