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.
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)?
Position
property instead ofLocation
? 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. – Wouter Kuijstersbest
, 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