5

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.

I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for. The awkwardly placed legend

Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?

2
  • 1
    Perhaps you could use the Position property instead of Location? This would require some minor tweaking. What you could do is make a list of, say, 4-6 positions where you think the position is OK, and then check your data for these positions. In the given plot for example, it is not too hard to compute that for the first 5 x values the lower range of your plot is available, and so that would be a suitable place to put the legend. Commented Mar 15, 2015 at 11:36
  • 1
    Can't try this right now - but what if you used best, determine the position of the legend (I am sure it is a property you can read), figure out what the nearest corner is then slide it over? Should be possible to make that a little function that you call with the desired axes as argument.
    – Floris
    Commented Mar 15, 2015 at 20:56

1 Answer 1

0

So, you have tried using Location instead of Position? For example:

x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')

and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:

lgd.Position

You should get something like this:

ans =

     0.7734    0.3037    0.1082    0.0200

which maps to:

[left bottom width height]

You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.

Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.

If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:

set(lgd,'Location','BestOutside')

Not the answer you're looking for? Browse other questions tagged or ask your own question.