具有外部化的 pgfplots 中的自定义图例顺序

具有外部化的 pgfplots 中的自定义图例顺序

我在 pgfplots groupplot 中很难获得自定义图例顺序。我试过了(esdd 的回答)但我收到了错误Undefined control sequence. {\patchFailedError}

Stefan Pinnow 后来给出了一个答案,它应该按以下方式工作:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{
    matrix,
    pgfplots.external,
    pgfplots.groupplots,
}
\pgfplotsset{compat=1.3}
\tikzexternalize

\begin{document}   
\begin{figure}
    \tikzset{external/force remake} 
    \tikzsetnextfilename{ext}
    \begin{tikzpicture}
        \begin{groupplot}[
            group style={
                group size= 2 by 1,
                xlabels at=edge bottom,
                ylabels at=edge left,
                x descriptions at=edge bottom,
            },
            ]
            \nextgroupplot[]
            \addplot [color=black] coordinates {(0,0) (1,0)};
            \addplot [color=red] coordinates {(0,1) (1,1)};
            
            \nextgroupplot[name=aa]
            \addplot [color=black] coordinates {(0,0) (1,0)}; \label{leg:a}
            \addplot [color=red] coordinates {(0,1) (1,1)}; \label{leg:b}
            
        \end{groupplot}
    
\matrix [
matrix of nodes,
nodes={anchor=west},
anchor=north east,
at={([shift={(-3pt,-3pt)}]aa.north east)},
fill=white,
draw,
inner sep=2pt,
row sep=2pt
] {
    \ref{leg:b} $b$ \\
    \ref{leg:a} $a$ \\
};

    \end{tikzpicture}
\end{figure}

\end{document}

但我得到了这个输出:

在此处输入图片描述

因此,绘图标记丢失了。我该如何实现自定义图例顺序?禁用外部化不可选(对于这个绘图也不适用)。

答案1

您必须定义node环境之外的位置,然后它才能按预期工作。

\documentclass{article}

\usepackage{pgfplots}

\usetikzlibrary{matrix}
\usepgfplotslibrary{groupplots,external}

\pgfplotsset{compat=newest}

\tikzexternalize

\begin{document}   
    \begin{figure}
        \tikzset{external/force remake} 
        \tikzsetnextfilename{ext}
        \begin{tikzpicture}
            \begin{groupplot}[
                group style={
                    group size= 2 by 1,
                    xlabels at=edge bottom,
                    ylabels at=edge left,
                    x descriptions at=edge bottom,
                },
                ]
                \nextgroupplot[]
                \addplot [color=black] coordinates {(0,0) (1,0)};
                \addplot [color=red] coordinates {(0,1) (1,1)};
                
                \nextgroupplot[name=aa]
                \addplot [color=black] coordinates {(0,0) (1,0)}; \label{leg:a}
                \addplot [color=red] coordinates {(0,1) (1,1)}; \label{leg:b}
                
            \end{groupplot}
            
            \matrix [
            matrix of nodes,
            anchor=north east,
            fill=white,
            draw,
            inner sep=2pt,
            row sep=2pt
            ] at {([shift={(-3pt,-3pt)}]aa.north east)} {
                \ref{leg:b} & $b$ \\
                \ref{leg:a} & $a$ \\
            };
            
        \end{tikzpicture}
    \end{figure}
    
\end{document}

在此处输入图片描述

相关内容