FontSize for the image's legend

조회 수: 3 (최근 30일)
Julián Francisco
Julián Francisco 2011년 11월 21일
I have the following code:
X = 0:pi/100:0.25*pi;
Y1 = sin(X);
Y2 = cos(X);
Y3 = tan(X);
fh = figure('toolbar','none','menubar','none','Units','characters');
Pan1 = uipanel(fh,'Units','normalized','Position',[0 0 0.5 1],'title',...
'Panel1');
Pan2 = uipanel(fh,'Units','normalized','Position',[0.5 0 0.5 1],'title',...
'Panel2');
haxes = axes('Parent',Pan2,'Units', 'normalized','Position',...
[0.125 0.1 0.75 0.75]);
hplot = plot(haxes,X,Y1,X,Y2,X,Y3);
xlabel(haxes,'Time (second)');
ylabel(haxes,'Amplitude (meter)');
title(haxes,'Trigonometric functions');
Ley = {'Sine function','Cosine function','Tangent function'}; %# legend's strings values
legend(haxes,Ley,'Location','SouthOutside');
[FileName,PathName,FilterIndex] = uiputfile('*.bmp;*.png;*.jpg;*.tif','Save as');
ftmp = figure('Menu','none','Toolbar','none','Units','normalized',...
'Position',[-1000 -1000 1 1]);
set(gcf,'PaperPositionMode','auto');
set(gcf,'InvertHardcopy','off');
new_axes = copyobj(haxes, ftmp);
legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
fmtgraf = {'-dbmp','-dpng','-djpeg','-dtiff'};
fmt = fmtgraf{FilterIndex};
print(ftmp,fmt,FileName,'-r0');
delete(ftmp);
delete(fh);
As seen in the code, the command line
legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);
is run before the command line
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
Because of it, the image appears cutted by its low part as seen here (independently of the existence or no existence of the property/value 'FontSize')
If the command line
legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);
is run after the command line
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
now the image is cutted by its low part but in this case it is not seen neither the xlabel text nor the legend box (as seen here)
If `'FontSize',8` is suppressed, all is Ok. How can I fix this if I want that the legend to have a lesser size?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 11월 21일
"the image file does not appear drawn correctly" is too vague for us to work with.
Julián Francisco
Julián Francisco 2011년 11월 23일
@Walter Roberson: I am sorry for my poor explanation. I have already fixed it (I have edited the question and I have added some images to improve it).

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Shub
Daniel Shub 2011년 11월 22일
If I understand correctly. You set the legend to be outside the figure, then set its text size, and then rescale the axis.
legend(haxes,Ley,'Location','SouthOutside');
legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
You are now upset that 0.1 normalized units does not provide enough space for a legend with 8 point font. There simply is not enough space to position everything optimally. What do you want MATLAB to do? Have you tried
set(new_axes,'Units','normalized','Position',[0.4 0.4 0.2 0.2]);
You might want to try one of the legend function on the FEX. Maybe http://blogs.mathworks.com/pick/2011/02/11/create-multi-column-plot-legends/
  댓글 수: 2
Julián Francisco
Julián Francisco 2011년 11월 22일
@Daniel: Thank you for your answer and link. I was thought of multicolumn plot legends but I am not sure if it was possible with Matlab.
Julián Francisco
Julián Francisco 2011년 11월 23일
@Daniel: I have done some proofs with the code and I have edited my question to include them. I think (although I am not sure) my problem has two causes: my widescreen size and the change of the property 'FontSize'. Respect to the latter, the Matlab documentation help says that "The font size and font name for the legend strings match the axes FontSize and FontName properties". So, if I change it through the command legend, then the axes' FontSize also changes.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2011년 11월 22일
I do not see any problems using Matlab 2009a.
The posted code seems to be more complicated than necessary. Are the UIPANELs required to reproduce the problem?
  댓글 수: 2
Julián Francisco
Julián Francisco 2011년 11월 23일
@Jan Simon: Thank you for your comment. Yes, the uipanels are necessary. I have just added two images to the question in order to improve my explanation.
Julián Francisco
Julián Francisco 2011년 11월 23일
@Jan Simon: I have done some proofs with the code and I have edited my question to include them. I think (although I am not sure) my problem has two causes: my widescreen size and the change of the property 'FontSize'. Respect to the latter, the Matlab documentation help says that "The font size and font name for the legend strings match the axes FontSize and FontName properties". So, if I change it through the command legend, then the axes' FontSize also changes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by