缩放使用 matlab2tikz 生成的 tikzpicture

缩放使用 matlab2tikz 生成的 tikzpicture

我有一个身影Plot1.tex我有一个用 matlab2tikz 生成的(Nico Schlömer 的 matlab2tikz)。我想将其包含在我的文档中并像这样缩放其宽度和高度:

\documentclass[a4paper]{report}
\usepackage{graphicx}
\usepackage{pgfplots} 

% Recommended settings according to matlab2tikz:
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\newlength\figureheight
\newlength\figurewidth

\begin{document}
\begin{figure}[ht]
    \centering
    \setlength\figureheight{7.0cm}
    \setlength\figurewidth{5.0cm}
    \input{Plot1.tex}
    \caption{Figure 1 caption.}
    \label{fig:fig1}
\end{figure}
\end{document}

但是,图形大小不受我以厘米为单位指定的值的影响。请告诉我我做错了什么。包括\textwidth代替固定长度的解决方案也值得赞赏。我也试过了,但也没成功。

编辑:文件的内容Plot1.tex如下所示。

% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
%  http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
\definecolor{mycolor2}{rgb}{0.85000,0.32500,0.09800}%
%
\begin{tikzpicture}

\begin{axis}[%
width=4.521in,
height=3.566in,
at={(0.758in,0.481in)},
scale only axis,
bar width=0.8,
xmin=-0.2,
xmax=8.2,
xtick={1, 2, 3, 4, 5, 6, 7},
xlabel style={font=\color{white!15!black}},
xlabel={Case},
ymin=0,
ymax=4500,
ylabel style={font=\color{white!15!black}},
ylabel={Amount},
axis background/.style={fill=white},
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot[ybar stacked, fill=mycolor1, draw=black, area legend] table[row sep=crcr] {%
1   3364.193\\
2   2593.019\\
3   2636.492\\
4   2691.691\\
5   2792.946\\
6   2962.925\\
7   3109.8\\
};
\addplot[forget plot, color=white!15!black] table[row sep=crcr] {%
-0.2    0\\
8.2 0\\
};
\addlegendentry{Part 1}

\addplot[ybar stacked, fill=mycolor2, draw=black, area legend] table[row sep=crcr] {%
1   704.461\\
2   1275.311\\
3   1251.84\\
4   1196.23\\
5   1108.538\\
6   983.136\\
7   891.978\\
};
\addplot[forget plot, color=white!15!black] table[row sep=crcr] {%
-0.2    0\\
8.2 0\\
};
\addlegendentry{Part 2}

\end{axis}
\end{tikzpicture}%

答案1

使用 ˙pgfplots 规定图表大小假设您的图像Plot1.tex是使用pgfplots包绘制的,并且没有在本地定义图像大小,那么您可以通过插入来定义图像大小

\pgfplotsset{height=5.0cm,width=7.0cm}

\input{Plot1.tex}

如果您使用standalone文档文件名绘制此图像Plot1.tex,它将帮助您获得以下 MWE:

\documentclass[a4paper]{report}
\usepackage{standalone} % <---
\usepackage{pgfplots}

% Recommended settings according to matlab2tikz:
\pgfplotsset{compat=1.16,
             plot coordinates/math parser=false}
% \newlength\figureheight  % not needed
% \newlength\figurewidth   % not needed

\begin{document}
\begin{figure}[ht]
    \centering
\pgfplotsset{height=5.0cm,width=7.0cm} % <---
    \input{Plot1.tex}
    \caption{Figure 1 caption.}
    \label{fig:fig1}
\end{figure}
\end{document}

其中文件Plot1.tex必须修改如下:

%%%% Plot1.tex
%---------------------------------------------------------------%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
\definecolor{mycolor2}{rgb}{0.85000,0.32500,0.09800}%
%
\begin{tikzpicture}

\begin{axis}[%
%width=4.521in,             % <--- remove
%height=3.566in,            % <--- remove
%at={(0.758in,0.481in)},    % <--- remove, it has no sense
scale only axis,
bar width=0.8,
xmin=-0.2,
xmax=8.2,
xtick={1, 2, 3, 4, 5, 6, 7},
xlabel style={font=\color{white!15!black}},
xlabel={Case},
ymin=0,
ymax=4500,
ylabel style={font=\color{white!15!black}},
ylabel={Amount},
axis background/.style={fill=white},
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot[ybar stacked, fill=mycolor1, draw=black, area legend] table[row sep=crcr] {%
1   3364.193\\
2   2593.019\\
3   2636.492\\
4   2691.691\\
5   2792.946\\
6   2962.925\\
7   3109.8\\
};
\addplot[forget plot, color=white!15!black] table[row sep=crcr] {%
-0.2    0\\
8.2 0\\
};
\addlegendentry{Part 1}

\addplot[ybar stacked, fill=mycolor2, draw=black, area legend] table[row sep=crcr] {%
1   704.461\\
2   1275.311\\
3   1251.84\\
4   1196.23\\
5   1108.538\\
6   983.136\\
7   891.978\\
};
\addplot[forget plot, color=white!15!black] table[row sep=crcr] {%
-0.2    0\\
8.2 0\\
};
\addlegendentry{Part 2}

\end{axis}
\end{tikzpicture}
%---------------------------------------------------------------%

现在主文件给出以下结果:

在此处输入图片描述

现在出现一个问题:由于您需要编辑所有这些文件,用所需的值(如果它们不同)替换 Matlab 确定的图表尺寸(高度、宽度)不是更简单吗?

相关内容