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,因此\refwill 只指向其中的最後一個,即最後一個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}

相關內容