GroupPlot 中缺少图例条目

GroupPlot 中缺少图例条目

我正在构建一个群图,其中的公共图例放置在图下的节点中。下面是一个重现我的问题的 MWE。

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotsset{compat = 1.12}

\begin{document}
    \centering
    \begin{figure}
    \begin{tikzpicture}{fig3}
        \begin{groupplot}[
            legend columns=4,
            legend entries={{\tiny Hold All},{\tiny Come and Go},{\tiny Random},{\tiny +Resources},{\tiny ++Resources},{\tiny ++ResourcesPT},{\tiny ++ResourcesSM},{\tiny ++ResourcesBM}},
            legend to name=Fig3Legend,
            group style={
                group name = fig3_plots,
                group size=3 by 1,
                xlabels at=edge bottom,
                ylabels at=edge left
            },
            width=\textwidth,
            legend style={draw=none},
            xlabel = {\footnotesize $\alpha$},
            ylabel = {\footnotesize Avg Portfolio Value},
        ]
        \nextgroupplot[title={\scriptsize +Resources},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0,
        xtick={1000,2000,3000,4000}]
            \addplot+[black, mark=o,line join=round, mark repeat=50] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[black, mark=x,line join=round, mark repeat=50] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[black, mark=|,line join=round, mark repeat=50] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[black, mark=square,line join=round, mark repeat=50] table[col sep=comma, y=Plus, x=Alpha]{ElicitedFixedAlpha.csv};
        \nextgroupplot[title={\scriptsize ++Resources (Empirical CDF)},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0]
            \addplot+[black, mark=o,line join=round, mark repeat=50] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=x,line join=round, mark repeat=50] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=|,line join=round, mark repeat=50] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=square,line join=round, mark repeat=50] table[col sep=comma, y=PlusPlus, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
        \nextgroupplot[title={\scriptsize ++Resources (Elicited CDFs)},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0]
            \addplot+[black, mark=o,line join=round, mark repeat=50] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=x,line join=round, mark repeat=50] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=|,line join=round, mark repeat=50] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=triangle,line join=round, mark repeat=50] table[col sep=comma, y=PlusPlusPT, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=square,line join=round, mark repeat=50] table[col sep=comma, y=PlusPlusSM, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[black, mark=asterisk,line join=round, mark repeat=50] table[col sep=comma, y=PlusPlusBM, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
        \end{groupplot}
        \node (fig3_Legend) at ($(fig3_plots c2r1.center)-(0,2.5cm)$){\ref{Fig3Legend}};
    \end{tikzpicture}
    \caption{Fixed $\alpha$ Values}
    \label{FixedAlphas}
\end{figure}
\end{document}

数据可从以下网址获取:

ElicitedFixedAlpha.txt

ElicitedFixedAlphaPlusPlus.txt

它产生以下输出

在此处输入图片描述

这很好,只是缺少最后两个图例条目。

在 groupplot 定义中我有

legend entries={{\tiny Hold All},{\tiny Come and Go},{\tiny Random},{\tiny +Resources},{\tiny ++Resources},{\tiny ++ResourcesPT},{\tiny ++ResourcesSM},{\tiny ++ResourcesBM}},

但正如您在图片中看到的,最后两个不在那里。相应的数据系列在图表中……很难看到,因为它们都在最右边的图表中彼此重叠,但它们在那里。那么为什么图例条目不显示呢?

答案1

您将在.log文件中发现警告

LaTeX Warning: Label `Fig3Legend' multiply defined.

LaTeX Warning: Label `Fig3Legend' multiply defined.

这意味着标签Fig3Legend应用于所有三个groupplots,因此\ref将仅指向其中最后一个,即最后一个groupplot。由于只有六个图,因此图例中会有六个条目。下面显示了一种解决方法。我首先为图定义了八种样式(为了方便起见,这不是必需的),然后在最后一个中groupplot添加了两个\addlegendimage,用于在图例中添加其他条目。

+Resources请注意,我还稍微改变了和的样式++Resources,这样混淆的可能性就更小了。您有三行具有相同的样式。

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotsset{compat = 1.12}

\begin{document}
    \begin{tikzpicture}[
            HA/.style={black, mark=o,line join=round, mark repeat=50},
            CAG/.style={black, mark=x,line join=round, mark repeat=50},
            Rnd/.style={black, mark=|,line join=round, mark repeat=50},
            P/.style={black, densely dashed, mark=square,line join=round, mark repeat=50},
            PP/.style={black, thick, mark=square,line join=round, mark repeat=50},
            PPPT/.style={black, mark=triangle,line join=round, mark repeat=50},
            PPSM/.style={black, mark=square,line join=round, mark repeat=50},
            PPBM/.style={black, mark=asterisk,line join=round, mark repeat=50}]

        \begin{groupplot}[
            legend columns=4,
            legend style={nodes={font=\tiny}},
            legend entries={Hold All,Come and Go,Random,+Resources,++Resources,++ResourcesPT,++ResourcesSM,++ResourcesBM},
            legend to name=Fig3Legend,
            group style={
                group name = fig3_plots,
                group size=3 by 1,
                xlabels at=edge bottom,
                ylabels at=edge left
            },
            width=\textwidth,
            legend style={draw=none},
            xlabel = {\footnotesize $\alpha$},
            ylabel = {\footnotesize Avg Portfolio Value},
        ]
        \nextgroupplot[title={\scriptsize +Resources},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0,
        xtick={1000,2000,3000,4000}]
            \addplot+[HA] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[CAG] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[Rnd] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlpha.csv};
            \addplot+[P] table[col sep=comma, y=Plus, x=Alpha]{ElicitedFixedAlpha.csv};
        \nextgroupplot[title={\scriptsize ++Resources (Empirical CDF)},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0]
            \addplot+[HA] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[CAG] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[Rnd] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[PP] table[col sep=comma, y=PlusPlus, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
        \nextgroupplot[title={\scriptsize ++Resources (Elicited CDFs)},
            y tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
      },
            footnotesize,
            x tick label style={
                font=\tiny,
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
        ymin=0]
            \addplot+[HA] table[col sep=comma, y=HoldAll, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[CAG] table[col sep=comma, y=ComeAndGo, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[Rnd] table[col sep=comma, y=Random, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addlegendimage{P}
            \addlegendimage{PP}
            \addplot+[PPPT] table[col sep=comma, y=PlusPlusPT, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[PPSM] table[col sep=comma, y=PlusPlusSM, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
            \addplot+[PPBM] table[col sep=comma, y=PlusPlusBM, x=Alpha]{ElicitedFixedAlphaPlusPlus.csv};
        \end{groupplot}
        \node (fig3_Legend) at ($(fig3_plots c2r1.center)-(0,2.5cm)$){\ref{Fig3Legend}};
    \end{tikzpicture}

\end{document}

相关内容