pgfplots 中的表格状图例

pgfplots 中的表格状图例

我使用legend style={legend columns=*}在 pgfplots 图例中创建列。但是,此选项只会更改图例条目的显示方式。我经常需要的是显示每个图的各种数据,就像在表格中一样。以下是我迄今为止使用的(丑陋的)解决方法的示例:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={$y=a \cdot x + b$},
xlabel=$x$,
ylabel=$y$,
legend pos=south east,
legend style={legend columns=2},
]

\addlegendimage{empty legend}
\addlegendimage{empty legend}
\addlegendentry{$\quad a \quad$} % \quad are just here to make columns wider... I suppose there's a better way
\addlegendentry{$\quad b \quad$}

\addplot+[domain=-1:2, samples=10] {3*x+1};
\addlegendimage{empty legend}
\addplot+[domain=-1:2, samples=10] {2*x+2};
\addlegendimage{empty legend}
\addplot+[domain=-1:2, samples=10] {1*x+3};
\addlegendimage{empty legend}

\addlegendentry{3}  % first data for the first graph
\addlegendentry{1}  % second data for the first graph
\addlegendentry{2}  % first data for the second graph
\addlegendentry{2}  % you've got it...
\addlegendentry{1}
\addlegendentry{3}

\end{axis}

\end{tikzpicture}
\end{document}

double entry legend

我将非常感激任何能让语法更清晰、最重要的是更强大的想法!

答案1

我认为最简单的解决方案是使用命令自己创建“图例” \matrix。要获得\addplot所需的样式\label并调用它们,请使用单元格\ref内的功能\matrix

有关更多详细信息,请查看代码中的注释。

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load `matrix' library so we can use the `matrix of nodes' feature
    \usetikzlibrary{
        matrix,
    }
    % use `compat' level 1.3 (or higher) to use the advanced placement features
    % for the axis labels
    \pgfplotsset{
        compat=1.3,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            title={$y=a \cdot x + b$},
            xlabel=$x$,
            ylabel=$y$,
        ]
            \addplot+ [domain=-1:2, samples=10] {3*x+1};
                % add labels to the plots
                \label{plot:line1}
            \addplot+ [domain=-1:2, samples=10] {2*x+2};
                \label{plot:line2}
            \addplot+ [domain=-1:2, samples=10] {1*x+3};
                \label{plot:line3}

            % create a (dummy) coordinate where we want to place the legend
            %
            % (The matrix cannot be placed inside the `axis' environment
            %  directly, because then a catcode error is raised.
            %  I guess that this is caused by the `matrix of nodes' feature)
            \coordinate (legend) at (axis description cs:0.97,0.03);
        \end{axis}

        % create the legend matrix which is placed at the created (dummy) coordinate
        % and recall the plot specification using the `\ref' command
        %
        % adapt the style of that node to your needs
        % (e.g. if you like different spacings between the rows or columns)
        \matrix [
            draw,
            matrix of nodes,
            anchor=south east,
        ] at (legend) {
                             & $a$ & $b$ \\
            \ref{plot:line1} & 3   & 1   \\
            \ref{plot:line2} & 2   & 2   \\
            \ref{plot:line3} & 1   & 3   \\
        };

    \end{tikzpicture}
\end{document}

image showing the result of above code

相关内容