41

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?

1

1 Answer 1

62

A doubtless naïve possibility:

\documentclass{article}

    % UNITS
\usepackage{siunitx}
\sisetup{per-mode=symbol}

    % 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

6
  • 3
    \pgfplotsset{width=7cm,compat=1.3} is important here. Without it, my axis doesn't show on the right. Commented Feb 10, 2017 at 0:28
  • @AndriyDrozdyuk thanks!
    – lhlmgr
    Commented Apr 18, 2020 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? Commented Nov 25, 2020 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
    Commented Nov 26, 2020 at 4: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
    Commented Feb 9, 2021 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .