18

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?

35

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

  • \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 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.