首页 > 解决方案 > Legend entries out of box

问题描述

I am using semilogx to plot data. Whenever I save the generated graph as an eps or a pdf file, legend entries go out the legend box (see the attachment). However, for .jpg it works fine. Please advise.

Following is the sample code that I use for plot:

[fa,xa] = ecdf(Variable_1);
[fb,xb] = ecdf(Variable_2);
set(0,'DefaultLineLineWidth',3)
set(0,'DefaultAxesFontName','Helvetica')
set(0,'DefaultAxesFontSize',18)
set(0,'DefaultTextFontName','Helvetica')
set(0,'DefaultTextFontSize',18)
semilogx(xa,fa,'b--');
hold all
semilogx(xb,fb,'r--');
hold all
legend({'Availability', 'Unavailability'},'location','northwest');
xlabel('Intervals (hours)');
ylabel('Cumulative fraction');
grid off

image

标签: matlabmatlab-figurelegend

解决方案


My first choice would be to get rid of the box:

legend({'Availability','Unavailability'},'location','northwest','box','off')

If you really want the box, you can manually increase its size, e.g. the increase width by 10%:

lposincr = [0 0 .1 0]; % left, bottom, width, height
l = legend({'Availability', 'Unavailability'},'location','northwest');
lpos = get(l,'Position');
set(l,'Position',lpos.*(1+lposincr))

推荐阅读