PGFplots:如何向“叠加”图添加图例

PGFplots:如何向“叠加”图添加图例

我在同一张图表上“叠加”了不同的图(即,我axis在一个环境中有两个 es 环境tikzpicture。)

在此处输入图片描述

现在我想为这两个图添加一个图例并将其放在图表外面(底部)。

我读过了如何从组图中的不同图获取图例条目?,但该解决方案对我来说不起作用,因为它涉及多个tikzpicture

这是我的 MWE:

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage{sansmath}
\usepackage{tikz}
\tikzset{%
  font=\sansmath\sffamily,
}
\usepackage{pgfplots}

\renewcommand{\familydefault}{\sfdefault}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    % legend columns=-1,           % <---- Doesn't work
    % legend entries={blue;,red},
    % legend to name=named,
    xlabel={Specific Impulse [s]},
    ylabel style={blue},
    y axis line style={blue}, 
    ytick style={blue}, 
    yticklabel style={blue},
    axis x line*=bottom, 
    axis y line*=left, 
    tick align = outside,
    scaled y ticks=base 10:-3,
    ]
    \addplot [
    color=blue,
    mark=*,
    line width=1.5pt,
    mark size=2.5pt,
    ] 
    table [x=Isp,y=mpropLEO] {OUTprop.dat};
  \end{axis}
  \begin{axis}[
    ylabel style={red},
    y axis line style={red}, 
    ytick style={red}, 
    yticklabel style={red},
    axis x line*=none,
    axis y line*=right,
    hide x axis,
    tick align=outside,
    ]
    \addplot [
    color=red,
    mark=square*,
    line width=1.5pt,
    mark size=2.5pt,
    ] 
    table [x=Isp,y=N] {OUTprop.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

OUTprop.dat文件为:

Isp      mpropLEO  N
200 296199.130625  0
225 178727.998632  -6
250 118317.300031  5
275  83818.963766  3
300  62498.670491  3
325  48488.756284  2
350  38822.104550  2
375  31882.745911  2
400  26735.688678  2
425  22812.101477  2
450  19750.977297  2
475  17314.797071  2
500  15342.358783  2

有人能帮我吗?

答案1

命令\addlegendentry\addlegendimage是你的朋友。你必须在最后一个图之前标记你正在使用的每个图。

% arara: pdflatex
% arara: pdflatex

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage{sansmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\tikzset{font=\sansmath\sffamily}

\usepackage{filecontents}
\begin{filecontents*}{OUTprop.dat}
    Isp      mpropLEO  N
    200 296199.130625  0
    225 178727.998632  -6
    250 118317.300031  5
    275  83818.963766  3
    300  62498.670491  3
    325  48488.756284  2
    350  38822.104550  2
    375  31882.745911  2
    400  26735.688678  2
    425  22812.101477  2
    450  19750.977297  2
    475  17314.797071  2
    500  15342.358783  2
\end{filecontents*}

\begin{document}    
    \begin{tikzpicture}
    \begin{axis}[
    xlabel={Specific Impulse in s},
    ylabel style={blue},
    y axis line style={blue}, 
    ytick style={blue}, 
    yticklabel style={blue},
    axis x line*=bottom, 
    axis y line*=left, 
    tick align = outside,
    scaled y ticks=base 10:-3,
    ]
    \addplot [
    color=blue,
    mark=*,
    line width=1.5pt,
    mark size=2.5pt,
    ] 
    table [x=Isp,y=mpropLEO] {OUTprop.dat};
    \label{plot_one}
    \end{axis}
    % % % % % % %
    \begin{axis}[%
    legend style={
        at={(0.5,-.25)},
        anchor=north},
    legend cell align=left,
    ylabel style={red},
    y axis line style={red}, 
    ytick style={red}, 
    yticklabel style={red},
    axis x line*=none,
    axis y line*=right,
    hide x axis,
    tick align=outside,
    ]
    \addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry[align=left]{veeeeeeeery\\blue}
    \addplot [
    color=red,
    mark=square*,
    line width=1.5pt,
    mark size=2.5pt,
    ] 
    table [x=Isp,y=N] {OUTprop.dat};
    \addlegendentry{normal red}
    \end{axis}
    \end{tikzpicture}   
\end{document}

在此处输入图片描述

相关内容