How do I move the legend box closer to the plot-edge? 2014b puts the legend box too far into the plot.

15 views (last 30 days)
Hi,
One of the big changes for me in version 2014b is that the legend box appears significantly further away from the plot edge, than it did in 2014a. Thus the legend box now obscures data it didn't just yesterday. :(
How can I decrease the buffer zone between the legend box and plot edge?
Yours, - Hrefna

Answers (7)

the cyclist
the cyclist on 9 Oct 2014
You might want to post an example of what you are seeing. My legend positioning seems fine.
But here's an example where I plunk the legend directly in the middle:
% Some data
x = 0:pi/10:pi;
y = sin(x);
% Create figure
figure
plot(x,y,x,2*y)
hL = legend({'1','2'});
% Get current position (which I used to keep overall size the same)
currentLegendPosition = hL.Position;
% Define new position
newLegendPosition = [0.5 0.5 currentLegendPosition([3 4])];
% Set new position
hL.Position = newLegendPosition;
% % (You could also use this syntax)
% set(hL,'Position',newLegendPosition)
  1 Comment
Hrefna
Hrefna on 9 Oct 2014
Hi,
I'm just using the basic legend 'location' tags, such as 'northwest' and 'southwest'. I'm plotting 3x2 subplots, then using orient tall before printing out to jpeg. Most of the plots are basically showing a line, y=ax+b.
Yesterday I made these plots with Matlab 2014a, and the legend box was sitting snugly in the corner, and didn't interfere with the data. Today, with 2014b, the legend box is significantly further away from the corner, and overlaps the data.
I'd hate to have to start nudging the legends manually (or semi-manually), I just want to shrink the built in buffer between the legend box and the plot-edge.
I wrote a little program that emulates my problem. There is a clear difference in the buffer between the plot-box and the legend-box when you run this in 2014a vs 2014b.
figure(1);clf
x = 1:10;
% Plot data in 3x2 subplots in one figure
for PlotNo = 1:6
% chose subplot
subplot(3,2,PlotNo)
% I have 3-5 lines of data in each plot
% Yes, I know this can be made neater using matrices, but I'm short on time.
for LineNo = 1:5
plot(x, (1+(rand(1)-0.5)*0.25)*x); hold all;
end
xlabel('x'); ylabel('y'); title('FunTimes!')
legend('Blah1dat','Blah2dat','Blah3dat','Blah4dat','Blah5dat','Blah6dat', 'location', 'northwest');
end
% print to jpg file
orient tall
print -djpeg LotsOfSubplots_LegendOnTop.jpg

Sign in to comment.


Doug Hull
Doug Hull on 9 Oct 2014
Edited: Doug Hull on 9 Oct 2014
I ran this code in both versions. Am I missing something? I would say they are functionally equivalent.
  2 Comments
Hrefna
Hrefna on 9 Oct 2014
Hi Doug, thanks for printing out the pics. I'll attach my own below. I should have done so earlier!
Is this equivalent? Well, you can see right off the bat that the new version puts the legend box much farther from the upper left corner.
When you have all the subplots squashed together like that, it doesn't matter, but I was printing these out using orient tall. That's when I noticed the buffer problem.
This is from 2014a:
This is from 2014b (I do like the bigger label fonts!):
Now, this is just one example, but it shows my basic problem: the legend box is much too far away from the plot-edge by default. I use the legend feature a lot, and I hate how much space it's taking up now.
Does anyone know how to change the default buffer? :)

Sign in to comment.


Doug Hull
Doug Hull on 9 Oct 2014
Now we see why this is important. It is not possible to change the margin padding (either the external or internal), but it is easy to adjust the font size such that the legend gets a little smaller.
legend({'Blah1dat','Blah2dat','Blah3dat','Blah4dat','Blah5dat','Blah6dat'} ,'location', 'northwest','fontSize',8)
When I make the font one point smaller, the legend gets smaller and then it fits for your data.
Also, instead of specifying the location as Northwest, have you considered 'Best'
  1 Comment
Hrefna
Hrefna on 9 Oct 2014
Sorry for taking so long to get my point across, but I'm glad I got there in the end! :)
I repeat that this issue isn't just for this particular example, it's for all my future plots, where I'll be gnashing my teeth at 'legend' because I know it could be tidier.
Now, making the legend text smaller is an option, but not a favorite of mine. I'd prefer being able to shrink the colored line in front of the text, or have the box closer to the edge.
So far my experience with 'best' has been, how shall I put this politely, bad. I reran the code using 'best' (and put 'orient tall' after the subplot command, just in case legend was interested in it) and this is the result:
Interestingly, you can see that the legend box is now MUCH CLOSER to the edge than before (kinda like in 2014a!! Where is this set?!), but overall this works even worse than the other ones. :/

Sign in to comment.


Doug Hull
Doug Hull on 9 Oct 2014
Edited: Doug Hull on 9 Oct 2014
I think I see why this looks different for both of us! My normal workflow is to use screenshots so I was posting what I saw on screen, you were posting what you print. I made my figure tall with just positioning on screen. Once I got it like I wanted, now to get a print, I do this.
set(gcf,'PaperPositionMode','auto');
print -djpeg LotsOfSubplots_LegendOnTop.jpg
This is more WYSIWYG. Try that.

Hrefna
Hrefna on 9 Oct 2014
Right. I tend to print straight to file (batch processing) and then scroll through the jpg-s later. Fixing everything by hand isn't really an option sometimes.
Anyway, I'm sorry to hear that this is just the way default-legend works now. I would rather not have to spend any more extra time on legends than I already do. :(

Doug Hull
Doug Hull on 9 Oct 2014
You have control over the position of the figure, and thus its size. Everything that I did for this particular figure could be automatic. Depending on the particulars of your specific data and figures, you might be able to set the positions to be tall on screen then print as I did to get good effects.

Hrefna
Hrefna on 9 Oct 2014
I'll look into it. That still doesn't change the fact that the basic default behavior of legend has been changed, and there's very little I can do about it besides spending (yet more) time noodling with the settings. I was hoping for something simple like being able to change the default margin to minimize the need for noodling.
Sigh.
That said I do like many of the other changes! Just not that one! :)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!