我正在尝试在我的代码中获得这种效果:
基本上,我想将标题和 x 轴标签移动到每行两个图之间的中心。
你能告诉我如何做到这一点吗?我看过帖子这里但仍然无法获得正确的输出
这是我的代码:
\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, matrix}
\pgfplotsset{compat=1.10, title/.append style={align =center}}
\begin{filecontents*}{data.csv}
Iter x1 x2 x3 x4 x5 x6
1, 0.9317521, 1.1849326, 1.6130556, 0.8022207, 0.9317521, 1.1849326
2, 1.8946202, 1.1228282, 1.8964566, -0.5353802, 1.8946202, 1.1228282
3, 1.5243302, 1.0372991, 1.4375012, 0.9719003, 1.5243302, 1.0372991
4, 1.5797030, 1.1346832, 1.8717142, 0.3138737, 1.5797030, 1.1346832
5, 1.8814457, 1.0529187, 2.0568468, -0.5509391, 1.8814457, 1.0529187
6, 2.0435003, 1.0470546, 2.0621956, -0.3565483, 2.0435003, 1.0470546
7, 2.0373926, 1.1215579, 2.1836100, 0.3360301, 2.0373926, 1.1215579
8, 1.9797077, 1.1632352, 1.8299063, 0.3871091, 1.9797077, 1.1632352
9, 1.9972528, 1.1952478, 1.8133509, -0.0867033, 1.9972528, 1.1952478
10, 1.8320176, 1.0625633, 1.0727495, 1.7256738, 1.8320176, 1.0625633
\end{filecontents*}
\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=2 by 3,
vertical sep=2cm,
horizontal sep = 2cm,
ylabels at=edge left},
width=7cm,
height=6cm,
try min ticks=5,
]
\nextgroupplot[align =center, title = {\bf{\emph{RSLE Errors}}\\$\mathbf{N(0,1000^2)}$}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot1}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};\label{plots:plot2}
\coordinate (top) at (rel axis cs:0,1);
\nextgroupplot[title = {\bf{\emph{MLS Errors}}\\},grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot3}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};
\nextgroupplot[title = {$\mathbf{N(0,0.001^2)}$}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};
\nextgroupplot[grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=3,col sep=comma] {data.csv};
\nextgroupplot[title = {$\mathbf{N(0,0.0001^2)}$}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=4,col sep=comma] {data.csv};
\nextgroupplot[grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=1,col sep=comma] {data.csv};
\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}
\path (top-|current bounding box.west)--
node[anchor=south,rotate=90] {\large{\bf{Absolute Parameter Error ($\mid$Estimated - Actual$\mid$})}}
(bot-|current bounding box.west);
% legend
\path (top|-current bounding box.north)--
coordinate(legendpos)
(bot|-current bounding box.north);
\matrix[
matrix of nodes,
anchor=south,
draw,
inner sep=0.2em,
draw
]at([yshift=1ex]legendpos)
{
\ref{plots:plot1}& curve 1&[5pt]
\ref{plots:plot2}& curve2&[5pt]\\};
\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}
答案1
添加group name=ape
到group style
,然后您可以将节点放置在相对于子图角落的位置,例如
\foreach \row in {1,2,3}
\path (ape c1r\row.south east) --node[below=0.5cm]{Number of recursions} (ape c2r\row.south west);
\path (ape c1r1.north east) --node[above]{$\mathbf{N(0,1000^2)}$} (ape c2r1.north west);
\path (ape c1r2.north east) --node[above]{$\mathbf{N(0,0.001^2)}$} (ape c2r2.north west);
\path (ape c1r3.north east) --node[above]{$\mathbf{N(0,0.0001^2)}$} (ape c2r3.north west);
另外,不要使用\bf
,而是{\bfseries bold stuff}
使用 或\textbf{bold stuff}
。
\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, matrix}
\pgfplotsset{compat=1.10, title/.append style={align =center}}
\begin{filecontents*}{data.csv}
Iter x1 x2 x3 x4 x5 x6
1, 0.9317521, 1.1849326, 1.6130556, 0.8022207, 0.9317521, 1.1849326
2, 1.8946202, 1.1228282, 1.8964566, -0.5353802, 1.8946202, 1.1228282
3, 1.5243302, 1.0372991, 1.4375012, 0.9719003, 1.5243302, 1.0372991
4, 1.5797030, 1.1346832, 1.8717142, 0.3138737, 1.5797030, 1.1346832
5, 1.8814457, 1.0529187, 2.0568468, -0.5509391, 1.8814457, 1.0529187
6, 2.0435003, 1.0470546, 2.0621956, -0.3565483, 2.0435003, 1.0470546
7, 2.0373926, 1.1215579, 2.1836100, 0.3360301, 2.0373926, 1.1215579
8, 1.9797077, 1.1632352, 1.8299063, 0.3871091, 1.9797077, 1.1632352
9, 1.9972528, 1.1952478, 1.8133509, -0.0867033, 1.9972528, 1.1952478
10, 1.8320176, 1.0625633, 1.0727495, 1.7256738, 1.8320176, 1.0625633
\end{filecontents*}
\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=2 by 3,
vertical sep=2cm,
horizontal sep = 2cm,
ylabels at=edge left,group name=ape},
width=7cm,
height=6cm,
try min ticks=5,
]
\nextgroupplot[align =center, title={\textbf{\emph{RSLE Errors}}}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot1}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};\label{plots:plot2}
\coordinate (top) at (rel axis cs:0,1);
\nextgroupplot[title = {\textbf{\emph{MLS Errors}}},grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot3}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};
\nextgroupplot[ grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};
\nextgroupplot[grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=3,col sep=comma] {data.csv};
\nextgroupplot[ grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=4,col sep=comma] {data.csv};
\nextgroupplot[grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=1,col sep=comma] {data.csv};
\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}
\foreach \row in {1,2,3}
\path (ape c1r\row.south east) --node[below=0.5cm]{Number of recursions} (ape c2r\row.south west);
\path (ape c1r1.north east) --node[above]{$\mathbf{N(0,1000^2)}$} (ape c2r1.north west);
\path (ape c1r2.north east) --node[above]{$\mathbf{N(0,0.001^2)}$} (ape c2r2.north west);
\path (ape c1r3.north east) --node[above]{$\mathbf{N(0,0.0001^2)}$} (ape c2r3.north west);
\path (top-|current bounding box.west)--
node[anchor=south,rotate=90] {\large{\bf{Absolute Parameter Error ($\mid$Estimated - Actual$\mid$})}}
(bot-|current bounding box.west);
% legend
\path (top|-current bounding box.north)--
coordinate(legendpos)
(bot|-current bounding box.north);
\matrix[
matrix of nodes,
anchor=south,
draw,
inner sep=0.2em,
draw
]at([yshift=1ex]legendpos)
{
\ref{plots:plot1}& curve 1&[5pt]
\ref{plots:plot2}& curve2&[5pt]\\};
\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}