2

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:

enter image description here

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.

1

1 Answer 1

2

It is really only the ordering. You need to add the images before the plots (in this case).

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\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},
        ]
        \addlegendimage{/pgfplots/refstyle=plot_red}\addlegendentry{red line}
        \addlegendimage{/pgfplots/refstyle=plot_blue}\addlegendentry{blue line}
        \addlegendimage{/pgfplots/refstyle=plot_green}\addlegendentry{green line}
        \addplot[green] coordinates{ (0,2) (1,3) (2,4) (3,5) }; \label{plot_green}


        \end{axis}

    \end{tikzpicture}

\end{document}  

enter image description here

3
  • Thank you, that works! But why? What changes when you move those lines? Commented Apr 20, 2019 at 17:12
  • 1
    @LarrySnyder610 A simplified explanation might be: when pgfplots "sees" \addlegendentry then it asks itself whether or not there is a plot which it could associate the entry to, meaning it looks if there has been a plot prior to \addlegendentry. If that's the case, it uses the style of the plot for the entry. This behavior is to make it most convenient to just write \addplot... \addlegendentry.... The price you pay for that convenience is that you need pay some attention to the ordering if you build up the legend differently.
    – user121799
    Commented Apr 20, 2019 at 17:23
  • Thank you so much, I would never have thought about this... it's actually super weird as the labels are now referenced before they are assigned, but indeed it works
    – F1iX
    Commented Jan 7 at 22:41

You must log in to answer this question.

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