Exporting figure to pdf: legend text extends beyond frame etc.

조회 수: 18 (최근 30일)
Brendan Finch
Brendan Finch 2016년 5월 23일
댓글: Brendan Finch 2016년 5월 23일
I would like to export a figure exactly as seen when opened in Matlab. However, in the resulting pdf file the legend text extends beyond the frame and the title partly overlaps with itself. These issues do not occur when exporting to png.
clc;close all;
d=dir('*.fig');
for i = 1:1%length(d)
fn=d(i).name;
myfn=openfig(fn)
%set(myfn,'PaperPositionMode','auto');
print(myfn,strrep(fn,'fig','pdf'),'-dpdf');
%saveas(gca, strrep(fn,'fig','pdf'), 'pdf');
close all
end
Any help would be appreciated, since I already tried several suggestions from the forum, but none worked so far.

채택된 답변

Richard Quist
Richard Quist 2016년 5월 23일
I think the issue you're seeing is because the font used in the generated PDF file is not the same as the font specified in the figure.
In the example figure you posted, both the title and the legend are using Calibri as the font. The exported PDF file uses Courier instead.
MATLAB supports a small set of fonts when exporting to PDF and PostScript, and Calibri is not one of the fonts that is supported. If the font isn't supported a substitution occurs when exporting, and in this case Courier is being used.
If you set the axes and legend text to use Helvetica instead you should see better results.
% this assumes ax and leg are handles to the axes and legend objects:
set(ax, 'FontName', 'Helvetica');
set(leg, 'FontName, 'Helvetica');
  댓글 수: 1
Brendan Finch
Brendan Finch 2016년 5월 23일
Thanks, that worked
This might be a bit overkill, but it's good enough:
clc;close all;
d=dir('*.fig');
for i = 1:length(d)
fn=d(i).name;
myfn=openfig(fn)
set(myfn,'PaperPositionMode','auto');
set(gca,'FontName', 'Helvetica')
axes = findobj(gcf,'type','axes')
set(findall(axes,'type','text'),'FontName', 'Helvetica')
set(findall(gcf,'type','text'),'FontName', 'Helvetica')
set(findall(myfn,'-property','FontName'),'FontName','Helvetica')
legend = findobj(gcf,'Type','legend')
set(legend,'FontName', 'Helvetica')
print(myfn,strrep(fn,'fig','pdf'),'-dpdf','-painters');
close all
end

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

추가 답변 (0개)

카테고리

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