question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:19
  • Comments:41 (18 by maintainers)

github_iconTop GitHub Comments

43reactions
salim-bcommented, Oct 16, 2017

+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 😉

9reactions
herrgruebnercommented, Jan 24, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Legends to Plots - R
This function can be used to add legends to plots. Note that a call to the function locator(1) can be used in place...
Read more >
Add legends to plots in R software : the easiest way! - STHDA
Example 1: line plot; Example 2: box plot. Infos. The goal of this article is to show you how to add legends to...
Read more >
Add legend to a plot in R - R Coder
A legend of a plot helps to understand which series or groups corresponds to each bar, line, box or observations, based on its...
Read more >
Andor iQ USER GUIDE
Move To and Set Reference - If a motorized XY stage is currently under iQ control, then images acquired with iQ have stage...
Read more >
R: Add Legends to Plots - MIT
Value · rect. a list with components. w , h. positive numbers giving width and height of the legend's box. left , top....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found