将一个图例拆分为两个图例

将一个图例拆分为两个图例

我试图将一个较长的图例拆分成两个图例。但是,我这样做只会填满整行。这就是我想要实现的目标:(我认为如果这些图标只是一个“x”或“三角形”,而没有一条线穿过它,那就更好了。)

在此处输入图片描述

我尝试添加一个带有新矩形的新图例并使用填充命令,但它会填充图表上的线。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{width=7cm,compat=1.3}
\hyphenation{}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    scaled x ticks=base 10:3,
}
\begin{axis}[
  axis y line*=left,
  ymin=0, ymax=210,
  ymajorgrids = true,
  legend style={at={(0.5,-0.10)},
      anchor=north,legend columns=1},%
  symbolic x coords={R2G,Gaussian,Box,Sobel, Edge Total,SIFT Total},
    xtick distance=1,
    %nodes near coords,
    ymode=log,
    x tick label style = {font=\small, rotate=45, anchor=east},
  ylabel=Energy Consumption (nanoJoule/Operation),
]

\addplot[nodes near coords style={anchor=west,rotate=60,inner xsep=5pt},smooth,mark=triangle,blue]
  coordinates{
    (R2G,29)
    (Gaussian,59)
    (Box,64)
    (Sobel,127)
    (Edge Total,210)
    (SIFT Total,861)
}; \label{CPU}


\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,red]
  coordinates{
    (R2G,7)
    (Gaussian,10)
    (Box,12)
    (Sobel,21)
    (Edge Total,46)
    (SIFT Total,7)
}; \label{GPU}

\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,black]
  coordinates{
    (R2G,20)
    (Gaussian,26)
    (Box,26)
    (Sobel,46)
    (Edge Total,20)
    (SIFT Total,91)
}; \label{FPGA}

\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,orange]
  coordinates{
    (R2G,22)
    (Gaussian,36)
    (Box,33)
    (Sobel,49)
    (Edge Total,153)
    (SIFT Total,102)
}; \label{HLS}
\end{axis}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
  symbolic x coords={R2G,Gaussian,Box,Sobel, Edge Total,SIFT Total},
 xtick=data,
  %nodes near coords,
  ymin=0, ymax=25,
  ymode=log,
  ylabel=Throughput (Gops)
]
\addlegendimage{/pgfplots/refstyle=CPU}\addlegendentry{CPU}
\addlegendimage{/pgfplots/refstyle=GPU}\addlegendentry{GPU}
\addlegendimage{/pgfplots/refstyle=FPGA}\addlegendentry{FPGA}
\addlegendimage{/pgfplots/refstyle=HLS}\addlegendentry{HLS}

\addplot [nodes near coords style={anchor=west,rotate=60,inner xsep=3pt},smooth,mark=x,blue]
  coordinates{
    (R2G,0.35)
    (Gaussian,0.35)
    (Box,0.32)
    (Sobel,0.32)
    (Edge Total,0.12)
    (SIFT Total,0.34)
};\addlegendentry{CPU (T)}

\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,red]
  coordinates{
    (R2G,1.48)
    (Gaussian,2.07)
    (Box,1.72)
    (Sobel,1.97)
    (Edge Total,24.88)
    (SIFT Total,1.57)
}; \addlegendentry{GPU (T)}

\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,black]
  coordinates{
    (R2G,0.51)
    (Gaussian,0.79)
    (Box,0.79)
    (Sobel,0.90)
    (Edge Total,8.70)
    (SIFT Total,0.79)
}; \addlegendentry{FPGA (T)}


\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,orange]
  coordinates{
    (R2G,0.47)
    (Gaussian,0.57)
    (Box,0.62)
    (Sobel,0.84)
    (Edge Total,1.13)
    (SIFT Total,0.71)
}; \addlegendentry{HLS (T)}

\end{axis}

\end{tikzpicture}
\end{document}

答案1

您有两个axis环境,因此您可以在每个环境中添加不同的图例,手动指定图例条目的样式。例如,在第一个axis添加中

\addlegendimage{mark=x, only marks} \addlegendentry{Energy}
\addlegendimage{mark=triangle, only marks} \addlegendentry{Throughput}

legend pos=north west并在选项中设置axis。在第二个选项中,添加

\addlegendimage{/pgfplots/refstyle=CPU, no markers, ultra thick}\addlegendentry{CPU}
\addlegendimage{/pgfplots/refstyle=GPU, no markers, ultra thick}\addlegendentry{GPU}
\addlegendimage{/pgfplots/refstyle=FGPA, no markers, ultra thick}\addlegendentry{FPGA}
\addlegendimage{/pgfplots/refstyle=HLS, no markers, ultra thick}\addlegendentry{HLS}

并使用legend style您已有的。(ultra thick当然,添加是可选的,从您的屏幕截图来看,您似乎希望图例中的线条更粗。)这不包括对标记的任何引用。解决该问题的一种方法是定义两种新样式,例如markA/.style={mark=x},并在图和图例中使用它。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{width=7cm,compat=1.3}
\hyphenation{}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    scaled x ticks=base 10:3,
}
\begin{axis}[
  axis y line*=left,
  ymin=0, ymax=210,
  ymajorgrids = true,
  legend pos=north west,
  symbolic x coords={R2G,Gaussian,Box,Sobel, Edge Total,SIFT Total},
  xtick distance=1,
    %nodes near coords,
  ymode=log,
  x tick label style = {font=\small, rotate=45, anchor=east},
  ylabel=Energy Consumption (nanoJoule/Operation),
]

\addlegendimage{mark=x, only marks} \addlegendentry{Energy}
\addlegendimage{mark=triangle, only marks} \addlegendentry{Throughput}

\addplot[nodes near coords style={anchor=west,rotate=60,inner xsep=5pt},smooth,mark=triangle,blue]
  coordinates{
    (R2G,29)
    (Gaussian,59)
    (Box,64)
    (Sobel,127)
    (Edge Total,210)
    (SIFT Total,861)
}; \label{CPU}


\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,red]
  coordinates{
    (R2G,7)
    (Gaussian,10)
    (Box,12)
    (Sobel,21)
    (Edge Total,46)
    (SIFT Total,7)
}; \label{GPU}

\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,black]
  coordinates{
    (R2G,20)
    (Gaussian,26)
    (Box,26)
    (Sobel,46)
    (Edge Total,20)
    (SIFT Total,91)
}; \label{FPGA}

\addplot[nodes near coords style={anchor=east,rotate=35,inner xsep=5pt},smooth,mark=triangle,orange]
  coordinates{
    (R2G,22)
    (Gaussian,36)
    (Box,33)
    (Sobel,49)
    (Edge Total,153)
    (SIFT Total,102)
}; \label{HLS}
\end{axis}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  legend style={at={(0.5,-0.22)},
      anchor=north,legend columns=-1},
  symbolic x coords={R2G,Gaussian,Box,Sobel, Edge Total,SIFT Total},
 xtick=data,
  %nodes near coords,
  ymin=0, ymax=25,
  ymode=log,
  ylabel=Throughput (Gops)
]

\addlegendimage{/pgfplots/refstyle=CPU, no markers, ultra thick}\addlegendentry{CPU}
\addlegendimage{/pgfplots/refstyle=GPU, no markers, ultra thick}\addlegendentry{GPU}
\addlegendimage{/pgfplots/refstyle=FGPA, no markers, ultra thick}\addlegendentry{FPGA}
\addlegendimage{/pgfplots/refstyle=HLS, no markers, ultra thick}\addlegendentry{HLS}

\addplot [nodes near coords style={anchor=west,rotate=60,inner xsep=3pt},smooth,mark=x,blue]
  coordinates{
    (R2G,0.35)
    (Gaussian,0.35)
    (Box,0.32)
    (Sobel,0.32)
    (Edge Total,0.12)
    (SIFT Total,0.34)
};

\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,red]
  coordinates{
    (R2G,1.48)
    (Gaussian,2.07)
    (Box,1.72)
    (Sobel,1.97)
    (Edge Total,24.88)
    (SIFT Total,1.57)
};

\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,black]
  coordinates{
    (R2G,0.51)
    (Gaussian,0.79)
    (Box,0.79)
    (Sobel,0.90)
    (Edge Total,8.70)
    (SIFT Total,0.79)
};


\addplot[nodes near coords style={anchor=north,rotate=0,inner xsep=3pt},smooth,mark=x,orange]
  coordinates{
    (R2G,0.47)
    (Gaussian,0.57)
    (Box,0.62)
    (Sobel,0.84)
    (Edge Total,1.13)
    (SIFT Total,0.71)
};

\end{axis}

\end{tikzpicture}
\end{document}

相关内容