不同地块的全局图例

不同地块的全局图例

我有多个轴环境,它们形成一个绘图数组。有时这些不同的轴环境绘制相同的数据,有时绘制不同的数据集。我想在所有 N 个子图的底部设置一个全局图例,每个具有唯一数据集的环境都有一个图例条目。我使用函数而不是表格数据将问题简化为以下两个子图。在此示例中,每个子图中都绘制了两个数据集。在第一个子图中,有两个唯一的绘图(标记为 p1 和 p2)。在第二个子图中,其中一个图(也是红色的)与 p1 相同,但第二个图对于前面的子图中的任何图都是唯一的。我想要一种优雅的方式来定义一个图例,每个图都有一个条目,标记为 p1 到 p3。

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{external} 
\pgfplotsset{width=10cm,
            compat=1.9,
            }

\tikzexternalize

\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{axis}[name=t1, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\end{axis}

\begin{axis}[name=t1a, at={($(t1.east)+(1cm,0cm)$)}, anchor=west, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1}; % same as p1
\addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
\end{axis}

\end{tikzpicture}

\end{document}

一定有办法定义一些图例并用来\ref{plotname}引用每条独特的线条或类似的东西。谢谢您的帮助!

答案1

最后也是最简单的解决方案就是将参考图放入一个节点\node[yshift=-1cm] at (9,0) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3};grouplot

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usetikzlibrary{calc} 
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=10cm,
            compat=1.9,
            }

\begin{document}

\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{groupplot}[group style={
        {group size=2 by 2}},name=t1, height=5cm, width=10cm,legend style={
        transpose legend,
        legend columns=0,
        draw=none }]
    \nextgroupplot[title=One, legend to name=grouplegend]
        \addplot[color=red]{x^2 - 1};\label{pgfplots:p1}
        \addlegendentry{$P1$}
        \addplot[color=blue]{e^x};\label{pgfplots:p2}
        \addlegendentry{$P2$}
        \addlegendimage{green}
        \addlegendentry{$P3$}

    \nextgroupplot[title=two]
        \addplot[color=red]{x^2 - 1}; % same as p1
        \addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
    
\end{groupplot}

\node[yshift=-20pt] at ($(group c1r1.south)!0.5!(group c2r1.south)$) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3};

\end{tikzpicture}

\end{document}

编辑:\node[yshift=-20pt] at ($(group c1r1.south)!0.5!(group c2r1.south)$) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3};

结果是:

在此处输入图片描述

答案2

另一种可能性是手动添加图例条目以\addlegendimage{green}达到\addlegendentry{$P3$}相同的效果。

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{external} 
\usetikzlibrary{calc} 
\pgfplotsset{width=10cm,
            compat=1.9,
            }

\pgfplotsset{
    legend image with text/.style={
        legend image code/.code={%
            \node[anchor=center] at (0.3cm,0cm) {#1};
        }
    },
}

\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{axis}[name=t1, height=5cm, width=10cm,legend style={at={(0.88,0.90)},anchor=north},
    legend cell align={left}]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\addlegendentry{$P1$}
\addlegendentry{$P2$}
\addlegendimage{green}
\addlegendentry{$P3$}

\end{axis}

\begin{axis}[name=t1a, at={($(t1.east)+(1cm,0cm)$)}, anchor=west, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1}; % same as p1
\addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2

\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

另一个版本groupplots

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc} 
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=10cm,
            compat=1.9,
            }



\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{groupplot}[group style={
        {group size=2 by 2}},name=t1, height=5cm, width=10cm,legend style={
        transpose legend,
        legend columns=0,
        draw=none }]
    \nextgroupplot[title=One, legend to name=grouplegend]
        \addplot[color=red]{x^2 - 1};\label{pgfplots:p1}
        \addlegendentry{$P1$}
        \addplot[color=blue]{e^x};\label{pgfplots:p2}
        \addlegendentry{$P2$}
        \addlegendimage{green}
        \addlegendentry{$P3$}

    \nextgroupplot[title=two]
        \addplot[color=red]{x^2 - 1}; % same as p1
        \addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
    
\end{groupplot}

    \node at (9,0)
    [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
    {\ref{grouplegend}};

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

您可以像平常一样绘制图例,\legend{P1,P2,P3}将图 P3 ( \addplot[color=green{e^-x};) 放在第一个图中,只是不要draw=none在可选参数中绘制它。图本身将消失,而图例中的条目将保留。

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{external} 
\usetikzlibrary{calc} 
\pgfplotsset{width=10cm,
            compat=1.9,
            }

%\tikzexternalize

\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{axis}[name=t1, height=5cm, width=10cm,legend style={at={(0.88,0.90)},anchor=north},
    legend cell align={left}]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\addplot[color=green,draw=none]{e^-x};\label{pgfplots:p3}
\legend{P1,P2,P3}
\end{axis}

\begin{axis}[name=t1a, at={($(t1.east)+(1cm,0cm)$)}, anchor=west, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1}; % same as p1
\addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2

\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

另一个版本grouplots

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc} 
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=10cm,
            compat=1.9,
            }



\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{groupplot}[group style={
        {group size=2 by 2}},name=t1, height=5cm, width=10cm,legend style={
        transpose legend,
        legend columns=0,
        draw=none }]
    \nextgroupplot[title=One, legend to name=grouplegend]
        \addplot[color=red]{x^2 - 1};\label{pgfplots:p1}
        \addlegendentry{$P1$}
        \addplot[color=blue]{e^x};\label{pgfplots:p2}
        \addlegendentry{$P2$}
        \addplot[color=green,draw=none]{e^-x};\label{pgfplots:p3}
        \addlegendentry{$P3$}

    \nextgroupplot[title=two]
        \addplot[color=red]{x^2 - 1}; % same as p1
        \addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
    
\end{groupplot}

    \node at (9,0)
    [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
    {\ref{grouplegend}};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容