I have a graph with three plots and two y-axes. Two of the plots use the left y-axis and one uses the right y-axis. In order to get a single legend with all three plots, I am using the ideas in this blog and this answer.
However, the legend entries are appearing next to the wrong plots:
I feel like I am following the idea faithfully, but obviously I'm doing something wrong. Can you help?
MWE:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis y line*=left,
ylabel={Red and Blue},
]
\addplot[red] coordinates{ (0,0) (1,1) (2,2) (3,3) }; \label{plot_red}
\addplot[blue] coordinates{ (0,1) (1,2) (2,3) (3,4) }; \label{plot_blue}
\end{axis}
\begin{axis}[
axis y line*=right,
ylabel={Green},
]
\addplot[green] coordinates{ (0,2) (1,3) (2,4) (3,5) }; \label{plot_green}
\addlegendimage{/pgfplots/refstyle=plot_red}\addlegendentry{red line}
\addlegendimage{/pgfplots/refstyle=plot_blue}\addlegendentry{blue line}
\addlegendimage{/pgfplots/refstyle=plot_green}\addlegendentry{green line}
\end{axis}
\end{tikzpicture}
\end{document}
By the way, if I put all three plots on the same y-axis, it works fine.