问题:我有 8 条线的图,需要将所有 8 条线的图例放入图中。文档格式为 IEEE,并且图需要适合单列。
当前的解决方案:我已经能够通过创建两个轴来拆分图,如下所示:
\begin{tikzpicture}
\begin{axis}[xlabel=Average Degree, ylabel=Total Weight, legend style={at={(.95,.69)}, label={[font=\footnotesize]left:K/Y+R}, font=\footnotesize, anchor=south east}, legend columns=2, cycle list name={four-1-0}]
\addplot+[grt] table [x=links, y=star-red]{\averageone};
\addplot+[grt] table [x=links, y=star-red]{\averagetwo};
\addplot+[grt] table [x=links, y=star-red]{\averagethree};
\addplot+[grt] table [x=links, y=star-red]{\averagefour};
\addplot+[inv] table [x=links, y=mat-red]{\averageone};
\addplot+[inv] table [x=links, y=mat-red]{\averagetwo};
\addplot+[inv] table [x=links, y=mat-red]{\averagethree};
\addplot+[inv] table [x=links, y=mat-red]{\averagefour};
\legend{(120),(120),(480),(960)}
\end{axis}
\begin{axis}[axis x line=none,axis y line=none, legend style={at={(.95,.68)}, label={[font=\footnotesize]left:DGMM+R}, font=\footnotesize, anchor=north east}, legend columns=2, cycle list name={four-0-1}]
\addplot+[inv] table [x=links, y=star-red]{\averageone};
\addplot+[inv] table [x=links, y=star-red]{\averagetwo};
\addplot+[inv] table [x=links, y=star-red]{\averagethree};
\addplot+[inv] table [x=links, y=star-red]{\averagefour};
\addplot+[bls] table [x=links, y=mat-red]{\averageone};
\addplot+[bls] table [x=links, y=mat-red]{\averagetwo};
\addplot+[bls] table [x=links, y=mat-red]{\averagethree};
\addplot+[bls] table [x=links, y=mat-red]{\averagefour};
\legend{,,,,(120),(120),(480),(960)}
\end{axis}
\end{tikzpicture}
第一个轴是可见的,第二个是不可见的。[inv]
样式创建了一条没有线条的线,[grn]
和[bls]
标签生成橙色和蓝色线条,循环列出four-0-1
并将four-1-0
不可见线条上的标记设置为none
。下面两行中有橙色线条,其圆圈颜色与蓝色线条上的方块相同,但在这张图片中很难看到。宏定义如下:
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage{tikz}
\pgfplotstableread{plts/experiment8b1_av.tab}\averageone
\pgfplotstableread{plts/experiment8b2_av.tab}\averagetwo
\pgfplotstableread{plts/experiment8b3_av.tab}\averagethree
\pgfplotstableread{plts/experiment8b4_av.tab}\averagefour
\pgfplotscreateplotcyclelist{four-1-0}{%
every mark/.append style={fill=teal}\\%
every mark/.append style={fill=green}\\%
every mark/.append style={fill=orange}\\%
every mark/.append style={fill=pink}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
}
\pgfplotscreateplotcyclelist{four-0-1}{%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=none}\\%
every mark/.append style={fill=teal}\\%
every mark/.append style={fill=green}\\%
every mark/.append style={fill=orange}\\%
every mark/.append style={fill=pink}\\%
}
\tikzstyle{bls}=[blue, solid, mark=square*]
\tikzstyle{grt}=[red, solid, mark=*]
\tikzstyle{inv}=[draw=none]
显然,除非你有相关的数据文件,但输出如下所示:
问题是:有没有一种优雅的方法可以在不构建两个轴的情况下完成与此非常相似的事情?在我的其他一些情节中,两个图例必须位于情节的完全不同的部分才能融入其中。
答案1
我来试试:这个解决方案不会创建正确的“图例”,而只是创建了带框的节点,因此您会失去所有不错的设置选项。它也可能通过更多的自动化(计数器、循环等)来解决。
\documentclass{minimal}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[label=l1]{0.1*x};
\label{p1}
\addplot{0.2*x};
\label{p2}
\addplot{0.3*x};
\label{p3}
\addplot{0.4*x};
\label{p4}
\addplot{0.5*x};
\label{p5}
\addplot{0.6*x};
\label{p6}
\end{axis}
% Draw first "Legend" node using a left justified shortstack, position using relative axis coordinates
\node [draw,fill=white] at (rel axis cs: 0.8,0.3) {\shortstack[l]{
\ref{p1} $0.1 \cdot x$ \\
\ref{p2} $0.2 \cdot x$ \\
\ref{p3} $0.3 \cdot x$}};
% Second "Legend" node
\node [draw,fill=white] at (rel axis cs: 0.3,0.8) {\shortstack[l]{
\ref{p4} $0.4 \cdot x$ \\
\ref{p5} $0.5 \cdot x$ \\
\ref{p6} $0.6 \cdot x$}};
\end{tikzpicture}
\end{document}