6

I have "superimposed" different plots on the same graph (i.e., I have two axises environments in one tikzpicture environment.)

enter image description here

Now I would like to add a single legend for both of the plots and place it outside (at the bottom) of the graph.

I have read How can legend entries be obtained from different plots in a groupplot?, but the solution doesn't work for me because it involves multiple tikzpictures.

Here is my 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}

and the OUTprop.dat file is:

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

Can someone help me?

Pier Paolo
  • 2,790

1 Answers1

4

The commands \addlegendentry and \addlegendimage are your friends. You will have to label each plot you are using before the last one.

% 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}

enter image description here

LaRiFaRi
  • 43,807
  • Thank you for your quick response. How can I place the legend outside (at the top or at the bottom of) the graph? Should I use legend pos (and where)? – Pier Paolo Dec 16 '14 at 11:34
  • 1
    @PierPaolo Sorry, needed a while to get that right. Put everything you need in the second axis! Please see my updated answer. – LaRiFaRi Dec 16 '14 at 11:42
  • One last thing: as my labels are pretty long, would it be possible to have the labels in different lines (i.e., breaking lines)? – Pier Paolo Dec 16 '14 at 11:47
  • 1
    @PierPaolo I copied the number of columns from your code... legend columns=1, (which is default) would be a start. If you want to do line breaks, try \parbox or alike. – LaRiFaRi Dec 16 '14 at 11:54
  • 1
    @PierPaolo of course, pgfplots had a solution for this. See my update. – LaRiFaRi Dec 16 '14 at 12:03
  • Perfect, thank you. Accepted as soon as I could test it. :) – Pier Paolo Dec 16 '14 at 12:30