Pgfplots 和 Groupplot:图例管理

Pgfplots 和 Groupplot:图例管理

我有以下 MWE:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgf,pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}


\begin{filecontents*}{data1.dat}
   t1       a1              t2       a2               t3        a3 
0.142    1.000           0.154    1.000            0.139     0.995
0.284    1.000           0.308    1.000            0.279     0.990
0.426    1.000           0.461    1.000            0.418     0.989
0.569    1.000           0.615    1.000            0.558     0.989
0.711    1.000           0.769    1.000            0.697     0.989
0.853    1.000           0.923    1.000            0.836     0.989
0.995    1.000           1.077    1.000            0.976     0.989
1.137    1.000           1.230    1.000            1.115     0.989
1.279    1.000           1.384    1.000            1.255     0.989
1.422    0.995           1.538    1.000            1.394     0.986
1.564    0.990           1.692    1.000            1.534     0.981  
\end{filecontents*}

\begin{filecontents*}{data2.dat}
   t1       a1              t4       a4               t3        a3
3.554    0.920           3.845    0.950            3.485     0.913
3.696    0.915           3.999    0.945            3.625     0.908
3.838    0.910           4.153    0.939            3.764     0.903
3.980    0.905           4.306    0.934            3.904     0.898
4.123    0.900           4.460    0.929            4.043     0.893
4.265    0.895           4.614    0.923            4.182     0.888
4.407    0.890           4.768    0.918            4.322     0.883
4.549    0.885           4.922    0.912            4.461     0.878
4.691    0.880           5.075    0.907            4.601     0.873
4.833    0.875           5.229    0.902            4.740     0.868
4.976    0.870           5.383    0.896            4.880     0.864
5.118    0.865           5.537    0.891            5.019     0.859
5.260    0.860           5.691    0.885            5.158     0.854    
\end{filecontents*}


\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.9]
\begin{groupplot}[group style={
    group name=rzrxcomp,
    group size=1 by 2,
    xlabels at=edge bottom,
    ylabels at=edge left,
    vertical sep=4em
    },
    xmin = 0, xmax = 2,
    ymin = 0.8, ymax = 1,
    xtick distance = 0.2,
    ytick distance = 0.2,
    xlabel={$time\;[s]$},
    ylabel={$mass$},
    grid = both,
    grid style = {dotted},
    minor tick num = 1,
    major grid style = {lightgray!75},
    minor grid style = {lightgray!75},
    width = 0.85\textwidth,
    height = 0.50\textwidth,
    %scale only axis,
    %legend cell align = {left},
    %legend pos = north east
    legend style={at={(0.5,-0.3)},
    anchor=north,legend columns=-1},
    legend cell align = {left},
]
    
 \nextgroupplot

\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat};
\addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};

\nextgroupplot[legend entries={n1,n2,n3,n4}]



\addlegendimage{black, very thick, no markers}
\addlegendimage{Lavender, very thick, no markers}
\addlegendimage{Violet, very thick, no markers}
\addlegendimage{BurntOrange, dotted, very thick, no markers}

 
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat};
\addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};
  

    %\legend{
    %n1,
    %n2,
    %n3,  
    %}
    
\end{groupplot}
\end{tikzpicture}
\end{figure}

\end{document}

现在想象一下,我们考虑:(t1,a1)=n1, (t2,a2)=n2, (t3,a3)=n3, (t4,a4)=n4。有没有办法在中添加第二列值集data2.dat,即(t4, a4)添加为n4在图例中?问题是我不知道如何在图例中添加n4不弄乱n2,与n4但首先\groupplot,我需要保留一个图例,用不同的相关颜色突出显示四个不同的图。

更新

根据@TorbjornT 的建议,我使用该\addlegendimage命令在一个图例中处理图的顺序和名称,效果很好。MWE 已更新了新部分。现在我还有一个问题,如何tick, very tick...在生成的图例中制作实线?该\addlegendimage命令似乎无法识别或接受这些样式特征。

相关内容