代码

代码

我在保持图的垂直对齐方面遇到了问题。此外,我的标题文本未居中。这是我在文档中得到的结果:

在此处输入图片描述

您能帮我让图表垂直对齐,让标题文字居中显示吗?谢谢。

以下是一些最少的代码:

\documentclass[11pt]{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\pgfplotsset{compat=1.8}

\begin{filecontents*}{data.csv}
  Iter   Abs1   Abs2      x      y
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738
\end{filecontents*}

%\pgfplotsset{grid style={dashed,gray}}
\pgfplotsset{minor grid style={dashed,red}}
\pgfplotsset{major grid style={dotted,green!50!black}}

\begin{document}

\begin{figure}[H]
\begin{center}

\begin{tikzpicture}
\begin{axis}[scale = .75, width=20cm,height=10cm, title = {\emph{(a) $P_{T}$ over different samples}}, xlabel={$x~Position$},ylabel={$y~Position$}]

% Graph column 2 versus column 0
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=3,y index=4,col sep=comma] {data.csv};
%\addlegendentry{Column 3}% y index+1 since humans count from 1

\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[grid = both,
    %minor tick num=0,
    every major grid/.style={gray, opacity=0.5},
    scale = .75, width=20cm,height=10cm, title = {\emph{(b) RSLE Parameter Errors in terms of Recursions RSLE}},xlabel={$Number~of~Recursions$},ylabel={Absolute Parameter Error}]

% Graph column 1 versus column 0
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};
\addlegendentry{$\delta_{r2}$}

% Graph column 1 versus column 0
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};
\addlegendentry{$\delta_{r3}$}
\end{axis}
\end{tikzpicture}
\caption[Plot showing (a) position ${\mathbf{P_{T}}}$ and (b)the Parameters Absolute Errors. ]{Plot showing (a) position ${\mathbf{P_{T}}}$ and (b) the Parameters Absolute Errors.}\label{abserror}
\end{center}
\end{figure}

\end{document} 

答案1

到目前为止,您的代码没有任何问题。但是,您声明了width=20cm,然后将其缩放到0.75我建议您设置width=\textwidthheight=75mm(或将高度保留为 100mm,如果您希望页面上只有这个图形,这从您的问题中看不清楚)并省略scale选项。

我稍微简化了你的代码:

\documentclass{book}
    \usepackage{pgfplots}
\pgfplotsset{compat=newest}
    \usepackage{filecontents}
    \usepackage{graphicx}
    \usepackage{float}

\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,
            headsep=10pt,a4paper,
            showframe]{geometry}

\begin{filecontents*}{data.csv}
  Iter  Abs1       Abs2       x          y
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738
\end{filecontents*}

%\pgfplotsset{grid style={dashed,gray}}
\pgfplotsset{minor grid style={dashed,red},
            major grid style={dotted,green!50!black},
            width=\textwidth, height=75mm,
            every major grid/.style={gray, opacity=0.5},
            y label style={at={(axis description cs:-0.08,.5)},
                           text depth=0.25ex, anchor=south,
                           },
            }

\begin{document}

\begin{figure}[htbp]
    \centering
\begin{tikzpicture}[mark = *,
every mark/.append style = {scale=0.65},
                    ]

\begin{axis}[
    grid,
    title = {\emph{(a) $P_{T}$ over different samples}}, 
    xlabel= {\emph{x Position}},
    ylabel= {\emph{y Position}},
            ]
% Graph column 2 versus column 0
\addplot [only marks,blue]
    table[x index=3,y index=4,col sep=comma] {data.csv};
%\addlegendentry{Column 3}% y index+1 since humans count from 1
\end{axis}
\end{tikzpicture}
    \vskip 5ex
\begin{tikzpicture}
\begin{axis}[
    grid = both,
    title = {\emph{(b) RSLE Parameter Errors in terms of Recursions RSLE}},
    xlabel= {\emph{Number of Recursions}}, 
    ylabel= {Absolute Parameter Error,
            ]

% Graph column 1 versus column 0
\addplot [only marks,red]
    table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};

% Graph column 1 versus column 0
\addplot [only marks,cyan] 
    table[x index=0,y index=2,col sep=comma] {data.csv};
\legend{$\delta_{r2}$, $\delta_{r3}$}
\end{axis}
\end{tikzpicture}
\caption{Plot showing (a) position ${\mathbf{P_{T}}}$ and (b) the Parameters Absolute Errors.}
    \label{abserror}
\end{figure}

这使:

在此处输入图片描述

我使用软件包的最新修订版本pgfplots(1.12.1,自 2015/05/02 起)。如您所见,对于放置,ylabel我使用 y 标签样式,其中我定义了它与图表的距离和最小文本深度(因此距离与当前字母如 g、j、q、p ... 或简单索引无关)。

编辑:我对 MWE 代码做了一些小小的简化:确定每个节点中的标记和标记样式我将其移入常见的 tikz 样式附加上\begin{tikzpicture}[<common styles>]

答案2

您可以使用 PGFplots groupplot。我曾经\textwidth设置过组的宽度,您不需要超过这个大小。

代码

\documentclass[11pt]{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.8}

\begin{filecontents*}{data.csv}
  Iter   Abs1   Abs2      x      y
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738
\end{filecontents*}

%\pgfplotsset{grid style={dashed,gray}}
\pgfplotsset{minor grid style={dashed,red}}
\pgfplotsset{major grid style={dotted,green!50!black}}

\begin{document}
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[group style={
                              group size=1 by 2,
                              vertical sep=2cm,
                              ylabels at=edge left},
                              width=\textwidth,
                              height=7cm]
                \nextgroupplot[title = {\emph{(a) $P_{T}$ over different samples}}, xlabel={$x~Position$},ylabel={$y~Position$}]{%
                % Graph column 2 versus column 0
                    \addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=3,y index=4,col sep=comma] {data.csv};
                }
                \nextgroupplot[title = {\emph{(b) RSLE Parameter Errors in terms of Recursions RSLE}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%        
                    % Graph column 1 versus column 0
                    \addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};
                    \addlegendentry{$\delta_{r2}$}

                    % Graph column 1 versus column 0
                    \addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};
                    \addlegendentry{$\delta_{r3}$}
                }
            \end{groupplot}
        \end{tikzpicture}
        \caption[Plot showing (a) position ${\mathbf{P_{T}}}$ and (b)the Parameters Absolute Errors. ]{Plot showing (a) position ${\mathbf{P_{T}}}$ and (b) the Parameters Absolute Errors.}\label{abserror}
    \end{figure}

\end{document}

结果

在此处输入图片描述

相关内容