plotly / plotly.js

Open-source JavaScript charting library behind Plotly and Dash
https://plotly.com/javascript/
MIT License
16.9k stars 1.85k forks source link

enable positioning of horizontal legend below xaxis label #6720

Open mzechmeister opened 1 year ago

mzechmeister commented 1 year ago

I want to put a (long) legend horizontally and below the xaxis.

But I have some trouble with positioning.

  1. By default, the legend runs into the axis label (see Fig. below).
  2. Putting the legend below the xaxis seems to require some guessing for a negative y (or are the better ways?
Plotly.newPlot('graph', [
   {  y: [2, 1, 3]},
   {  y: [4, 2, 5]},
   {  y: [3, 3, 6]},
  ],
  {xaxis: {title: {text: 'axis label'}},
           legend: {title: {text: 'legend title'},
                    orientation: 'h'}
  }
)

grafik

https://jsfiddle.net/5udmbkza

28raining commented 1 year ago

Is positioning it like this OK?

https://jsfiddle.net/f9mxnras/4/

Just adding side:'top' to legend.title makes the automatic placement acceptable

mzechmeister commented 1 year ago

@28raining I want to put the legend below the graph (not inside).

28raining commented 1 year ago

This then? https://jsfiddle.net/L519hxk4/

In your original post you said you had to guess. That's correct, plotly team decided to use X, Y, Xanchor and Yanchor to position the title... not text like 'below' or 'middle'

mzechmeister commented 1 year ago

Now you use side: 'top' and y=-1 yielding a rather large gap towards the axis label. You see it's guessing and not fully clear what will happen, if the graph is resized or font size changes. And the default as shown above is unpleasent, too.

I think, what is missing, might be to specify y in terms of char heights or an additional coordinate system (graph, screen), which refers to the outer border. (There is a xref = ‘paper’, but seems to be just relative graph coordinates).

28raining commented 1 year ago

Are you requesting feature; instead of writing Y=-0.2, it's possible to write Y=-2*char ?

Because I think the Y=-0.2 solution works perfectly, I won't be working on this. Feel free to git clone and create a PR to add this feature.

mzechmeister commented 1 year ago

So, yeah if there is no convenient solution, it seems I'm requesting a feature and leave it the experts familiar with code.