将公共图例移至 pgfplots 中图表的右上角

将公共图例移至 pgfplots 中图表的右上角

在下面的代码中,我希望得到一些帮助将我的常见图例移动到情节的右上角,如下所示:

在此处输入图片描述

你能帮助我吗?谢谢。

这是我的代码:

\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}}
\pgfkeys{/pgf/number format/.cd,fixed,precision=3}

\begin{filecontents*}{data.csv}
  Iter  abs11  abs21  abs12  abs22
     1, 0.0016930, 0.0007109, 0.0169301, 0.0071087
     2, 0.0004656, 0.0006430, 0.0046563, 0.0064301
     3, 0.0001496, 0.0010624, 0.0014964, 0.0106239
     4, 0.0000946, 0.0006596, 0.0009458, 0.0065962
     5, 0.0000274, 0.0002370, 0.0002737, 0.0023697
\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= 1  by 2,
  vertical sep=2cm,
  horizontal sep = 2cm,
  ylabels at=edge left,group name=ape},
  width=15cm,
  height=8cm,
  try min ticks=5,
  scaled ticks=false,
  ]
\nextgroupplot[align =center,ymin=0, ymax=0.02,
title={\textbf{\emph{(a) RSLE Absolute Errors with $\mathbf{N(0,0.001^{2})}$}}},
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[ymin=0, ymax=0.02,
title = {\textbf{\emph{(b) RSLE Absolute Errors with $\mathbf{N(0,0.01^{2})}$}}},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=3,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=4,col sep=comma, green, only marks] {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}& $\delta r_{2}$ &[5pt]
\ref{plots:plot2}& $\delta r_{3}$ &[5pt]\\};
\end{tikzpicture}
\caption[Plot showing Absolute Errors with noise with parameters for RLSE.]{Plot showing RLSE Absolute Errors with noise with parameters (a) $N(0,0.001^{2})$ and (b) $N(0,0.001^{2})$.}\label{abserror}
\end{figure}

\end{document} 

答案1

将其放置在ape r1c1.north east,与anchor=south east

请注意,\large不是一个接受参数的命令,它是一个开关,因此应该用作{\large text}而不是\large{text}。 并且\bf已被弃用,如上一个问题所述。

\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}}
\pgfkeys{/pgf/number format/.cd,fixed,precision=3}

\begin{filecontents*}{data.csv}
  Iter  abs11  abs21  abs12  abs22
     1, 0.0016930, 0.0007109, 0.0169301, 0.0071087
     2, 0.0004656, 0.0006430, 0.0046563, 0.0064301
     3, 0.0001496, 0.0010624, 0.0014964, 0.0106239
     4, 0.0000946, 0.0006596, 0.0009458, 0.0065962
     5, 0.0000274, 0.0002370, 0.0002737, 0.0023697
\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= 1  by 2,
  vertical sep=2cm,
  horizontal sep = 2cm,
  ylabels at=edge left,group name=ape},
  width=15cm,
  height=8cm,
  try min ticks=5,
  scaled ticks=false,
  ]
\nextgroupplot[align =center,ymin=0, ymax=0.02,
title={\textbf{\emph{(a) RSLE Absolute Errors with $\mathbf{N(0,0.001^{2})}$}}},
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}

\nextgroupplot[ymin=0, ymax=0.02,
title = {\textbf{\emph{(b) RSLE Absolute Errors with $\mathbf{N(0,0.01^{2})}$}}},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=3,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=4,col sep=comma, green, only marks] {data.csv};
\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}

\path (ape c1r1.north west-|current bounding box.west)--
  node[anchor=south,rotate=90] {\large\textbf{Absolute Parameter Error ($\mid$Estimated - Actual$\mid$})}
  (ape c1r2.south east-|current bounding box.west);
% legend

\matrix[
matrix of nodes,
anchor=south east,
draw,
inner sep=0.2em,
draw
  ] at(ape c1r1.north east)
  {
\ref{plots:plot1}& $\delta r_{2}$ &[5pt]
\ref{plots:plot2}& $\delta r_{3}$ &[5pt]\\};
\end{tikzpicture}
\caption[Plot showing Absolute Errors with noise with parameters for RLSE.]{Plot showing RLSE Absolute Errors with noise with parameters (a) $N(0,0.001^{2})$ and (b) $N(0,0.001^{2})$.}\label{abserror}
\end{figure}

\end{document} 

相关内容