来源
MATLAB 仿真完,想把结果导出矢量图,放 latex 中写论文
方法一
指定 render 方式为 vector
print(Fig, FileName,'-dpdf','-r0', '-vector')
或者指定 Fig 的 render 为 painters
Fig.Renderer = 'painters';
方法二
MATLAB2020a 引入exportgraphics
函数,但是导出 PDF 矢量图 背景设置透明不起作用。据说 eps 等其他矢量图格式可以透明背景。
exportgraphics(Fig_Paper,'BB.pdf','BackgroundColor','none','ContentType','vector')
方法三 透明背景
最近看到 matlab 论坛 有人分享一个函数 export_fig, 相应透明背景设置在 export_fig Transparency。
set(gca,'xtick',[],'xticklabel',[]);
set(gca,'ytick',[],'yticklabel',[]);
set(gca, 'Color', 'none'); % Sets axes background
set(gcf, 'Color', 'none'); % Sets fig. background
axis off;
export_fig('test2.png', '-transparent','-r300');
% export_fig test.png -transparent -r300;
自己封装导出PDF的函数
function [] = PrintFig2PDF(Fig, FileName)
% for exporting Fig to PDF for paper
% This function will adjust the configuration for exporting Figure to PDF,
% mainly to get rid of the large blank margin of the generated PDF.
%
% Attention:
% [1] try to avoid adjusting the size of the figure after this function.
%
% Input:
% FileName -- name of the PDF, containing the path of the folder
% Fig -- handle of the figure for PDF
%
if nargin == 1
if isa(Fig,'matlab.ui.Figure')
FileName = 'New';
elseif isa(Fig,'char')
FileName = Fig;
Fig = gcf;
end
elseif nargin == 2
if isa(Fig,'char')
temp = Filename;
FileName = Fig;
Fig = temp;
end
elseif nargin <1
Fig = gcf;
FileName = 'New';
end
set(Fig,'Units','Inches');
% Fig.Renderer = 'vector'; % 这个可以是 3D plot 输出矢量图
pos = get(Fig,'Position');
set(Fig,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
%% Ver2023 and its previous release are different in print function
ver = version('-date');
if str2num(ver(end-4:end))<2023
print(Fig, FileName,'-dpdf','-r0', '-painters')
else
print(Fig, FileName,'-dpdf','-r0', '-vector')
% another method exportgraphics ver>2020a
% exportgraphics(Fig,[FileName,'.pdf'],'BackgroundColor','none','ContentType','vector')
end
end
其他方法
参考 print 函数,可以输出 eps, svg, 等矢量图
print(Fig1, 'ChenFig1', '-depsc', '-r600')
print(Fig1, 'ChenFig1', '-dmeta', '-r600')
print(Fig1, 'ChenFig1', '-dsvg', '-r600')
print(Fig1, 'ChenFig1', '-dpsc', '-r600')