我正在MATLAB中生成一些图形,然后尝试将其插入LaTeX。但是,当我这样做时,它们通常没有相同的尺寸(尽管我使用相同的设置来生产它们)。
例如:
我当前使用的MATLAB代码是这一
lsize = 16; % Label fontsize
nsize = 16; % Axis fontsize
q=randn(100,1000);
a1=linspace(1,1000,1000);
b1=linspace(2,2000,1000);
figure (1)
histogram(q)
xlabel('Time [sec]','Fontsize', lsize)
ylabel('W_{kin} [keV]','Fontsize', lsize)
set(gca, 'Fontsize', nsize)
set(gcf,'paperpositionmode','auto');
set(gcf,'windowstyle','normal');
set(gca,'LooseInset',max(get(gca,'TightInset'), 0.02))
set(gca,'fontweight','normal')
opts.Colors = get(groot,'defaultAxesColorOrder');
opts.saveFolder = 'img/';
opts.width = 12;
opts.height = 10;
opts.fontType = 'Times';
saveas(gcf,'f1.png')
figure(2)
loglog(a1,b1)
xlabel('time [sec]','Fontsize', lsize)
ylabel('Speed [m/sec]','Fontsize', lsize)
set(gca, 'Fontsize', nsize)
set(gcf,'paperpositionmode','auto');
set(gcf,'windowstyle','normal');
set(gca,'LooseInset',max(get(gca,'TightInset'), 0.02))
set(gca,'fontweight','normal')
opts.Colors = get(groot,'defaultAxesColorOrder');
opts.saveFolder = 'img/';
opts.width = 12;
opts.height = 10;
opts.fontType = 'Times';
saveas(gcf,'f2.png')
我正在使用的乳胶代码是:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % needed for figures
\begin{document}
\begin{figure}[!ht]
\begin{center}
\includegraphics[width=0.3\textwidth]{f2.png}\\
\includegraphics[width=0.3\textwidth]{f1.png}
\caption {A caption}\label{A_label}
\end{center}
\end{figure}
\end{document}
难道我做错了什么?
尝试在LaTeX中绘制海量数据(不是专为它而做)时,我遇到了类似的问题,因此我想在MATLAB中绘制图形并在LaTeX中排列它们(和轴)。因此,我将print
它们编辑为PDF。
始终要满足精确图形尺寸的技巧是使轴以填充整个图形set(gca,'position',[0 0 1 1])
。您需要绘制坐标轴,蜱,和标签乳胶(记得要使用的选项axis on top
在pgfplots
那里)。
function printFig2PDF(fh,FigName,FigWidth,FigHeight)
%% export MATLAB-figure as PDF
Format = 'pdf';
% check if input name has an extension
lst = strsplit(FigName,'.');
if ~strcmpi(lst{end},Format)
% append format
FigName = strcat(FigName,'.',lower(Format));
end
%% adjust figure
if ~isempty(fh.ax.Legend)
fh.ax.Legend.Visible = 'off';
end
fh.ax.Box = 'off';
set( fh.ax, 'YTickLabel',{},'XTickLabel',{});
set( fh.ax, 'yColor','none','xColor','none');
set(fh.ax, 'Position',[0 0 1 1])
set(fh.fig, 'PaperUnits','centimeters',...
'PaperPosition',[0 0 FigWidth FigHeight],...
'PaperSize',[FigWidth FigHeight]);
% save as PDF
print(fh.fig,FigName,'-dpdf')
% close figure handle
close(fh.fig)
end
请注意,我假设第一个输入(fh
)是一个struct
带有fig
图形手柄并ax
包含轴手柄的字段(如果我有多个图形和子图,这就是我存储这些手柄的方式)。如果只想绘制一个轴的当前图形,则可以使用
fh = struct('fig',gcf, 'ax',gca);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句