40

After googling about my problem, I came up with this two year old question here on TeX.SX.
I adopted the example from there having two plots in the first axis-environment and one plot in the second axis-environment as given below:

\documentclass{article}

    % UNITS
\usepackage{siunitx}
\sisetup{per=slash, load=abbr}

    % GRAPHICS
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.3}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    scaled x ticks=base 10:3,
    xmin=0, xmax=0.06
}

\begin{axis}[
  axis y line*=left,
  ymin=0, ymax=80,
  xlabel=x-axis,
  ylabel=y-axis 1,
]
\addplot[smooth,mark=x,red] 
  coordinates{
    (0,68.6)
    (0.0148,72) 
    (0.0295,68.6)
    (0.0441,53.4)
    (0.059,22.8) 
}; \label{plot_one}
%\addlegendimage{/pgfplots/refstyle=Hplot}\addlegendentry{plot 1}
\addplot[smooth,mark=o,green] 
  coordinates{
    (0,60)
    (0.0148,65) 
    (0.0295,73)
    (0.0441,49)
    (0.059,19) 
}; \label{plot_two}
%\addlegendentry{plot 2}
\end{axis}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  ymin=0, ymax=100,
  ylabel=y-axis 2
]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addplot[smooth,mark=*,blue] 
  coordinates{
    (0,0)
    (0.0148,48) 
    (0.0295,66)
    (0.0441,66)
    (0.059,45.0) 
}; \addlegendentry{plot 3}
\end{axis}

\end{tikzpicture}
\end{document}

There is a comment about my problem beneath the answer of the old question linked to above, but I do not get this hint working. So, how can I get one legend containing all three "descriptions" of the three plots?

albert
  • 937
  • http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855#148855 might help. – John Kormylo Sep 01 '14 at 21:32

1 Answers1

61

A doubtless naïve possibility:

\documentclass{article}

    % UNITS
\usepackage{siunitx}
\sisetup{per=slash, load=abbr}

    % GRAPHICS
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.3}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    scaled x ticks=base 10:3,
    xmin=0, xmax=0.06
}

\begin{axis}[
  axis y line*=left,
  ymin=0, ymax=80,
  xlabel=x-axis,
  ylabel=y-axis 1,
]
\addplot[smooth,mark=x,red]
  coordinates{
    (0,68.6)
    (0.0148,72)
    (0.0295,68.6)
    (0.0441,53.4)
    (0.059,22.8)
}; \label{plot_one}

\addplot[smooth,mark=o,green]
  coordinates{
    (0,60)
    (0.0148,65)
    (0.0295,73)
    (0.0441,49)
    (0.059,19)
}; \label{plot_two}

\end{axis}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  ymin=0, ymax=100,
  ylabel=y-axis 2
]
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{plot 2}
\addplot[smooth,mark=*,blue]
  coordinates{
    (0,0)
    (0.0148,48)
    (0.0295,66)
    (0.0441,66)
    (0.059,45.0)
}; \addlegendentry{plot 3}
\end{axis}

\end{tikzpicture}
\end{document}

gives:

3 plots, 2 axes, 1 legend, 3 descriptions

jo3ran
  • 65
cfr
  • 198,882
  • 3
    \pgfplotsset{width=7cm,compat=1.3} is important here. Without it, my axis doesn't show on the right. – Andriy Drozdyuk Feb 10 '17 at 00:28
  • @AndriyDrozdyuk thanks! – lhlmgr Apr 18 '20 at 14:09
  • Does not work for me. I have strange problems that the legend entries for plot 1 and plot 2 only look right after some compilations.

    For the record, i've compiled the example in ktikz. So maybe this is a LaTeX label problem?

    – Marvin Noll Nov 25 '20 at 16:49
  • @MarvinNoll I don't know anything about ktikz. I just copied the code above back to a test file and compiled. I get the output shown above. You do have to compile more than once. That's not a 'strange problem'. That's perfectly normal. As I say, I don't know about ktikz. – cfr Nov 26 '20 at 04:43
  • 1
    make sure to compiler twice otherwise the reference cannot be resolved. otherwise the first referenced line will just be a default black line. – jack Feb 09 '21 at 14:32
  • @jack it works when I am in a short tex file. But when I insert the tikzpicture code into a long tex file and run 2 times (and even more), the reference is not resolved. – el_maxx Nov 01 '22 at 15:29