# pgfplots: Legends in multiple y-axis plot overlapping

I am trying to add a legend for two plots on two different y-axes, however they are overlapping each other and each one is being written in its own box. I would like for both of them to share a single box and not overlap each other.

Most of the examples I have seen for creating the legend assume that all series happen within a single axis environment, but creating a second y-axis requires me to also create a second axis environment, and I am guessing that's where I run into trouble.

Here's a MWE. What am I overlooking?

\documentclass{article}

% UNITS
\usepackage{siunitx}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
scale only axis,
scaled x ticks=base 10:3,
axis y line*=left,
xmin=0, xmax=0.06,
ymin=0, ymax=80,
xlabel=$Q/\si{\metre\cubed\per\second}$,
ylabel=$H/\si{\metre}$]
coordinates{
(0,68.6)
(0.0148,72)
(0.0295,68.6)
(0.0441,53.4)
(0.059,22.8)
};
%        \addlegendentry{$H$}
\legend{$H$}
\end{axis}

\begin{axis}[
scale only axis,
scaled x ticks=base 10:3
/pgf/number format/sci subscript,
axis y line*=right,
axis x line=none,
xmin=0, xmax=0.06,
ymin=0, ymax=100,
ylabel=$\eta/\si{\percent}$]
coordinates{
(0,0)
(0.0148,48)
(0.0295,66)
(0.0441,66)
(0.059,45.0)
};
%      \addlegendentry{$\eta$}
\legend{$\eta$}
\end{axis}

\end{tikzpicture}
\end{document}

-
This doesn't answer your question directly, but if you're open to putting the legend in the caption, you can do so by adding \label{plot:one} immediately after the first \addplot ...; and then \ref{plot:one} in the caption. –  cmhughes Jan 29 '12 at 21:23
Oddly enough, this does not work for me. I looked up the syntax and proper use in the pgfplots manual, and still, nothing. All I get is an unresolved-reference error. –  Ricardo Jan 30 '12 at 2:44
It worked fine for me- see Jake's answer to this question. The problem with your current approach is that the legends are overlaying each other. Another work around is to position the separate legends in different locations in the graph- this may be a good idea, as it might help the reader determine which y-axis to look at. You can add legend pos=north west to the first axis environment, for example, so that \eta is in the top left, and H is in the top right –  cmhughes Jan 30 '12 at 4:12
I figured out what was going wrong here, though not how to fix it. I tested the plot in an article class, but my document is a tufte-handout class, that places the captions in the margin. In the article class it works as expected, but in the tufte-handout even when I tried protecting the \ref as recommended in the pgfmanual, it still did not work. I like the idea of placing each serie's legend close to it's axis. I'll try it. –  Ricardo Jan 30 '12 at 19:47
It works well, but doesn't seem to suit this particular plot well. –  Ricardo Jan 30 '12 at 20:28

You could alter the legend of the second plot "by hand" by using \addlegendimage{<plot options>} \addlegendentry{<plot title>} after the second plot. You can access the <plot options> of the first plot by assigning a \label{<name>} to it and using the key /pgfplots/refstyle=<name> in your \addlegendimage command:

Another option would be to label the plots directly. Since pgfplots version 1.5.1, you can place nodes a specified length along the plot by including a node [pos=<fraction>] {} in the \addplot command.

Depending on how you're going to use the graph, you could also consider colouring the axis to match the plots. This will be problematic if you need to print the document in black and white, but in a presentation, this might be more elegant than forcing the audience's eyes from the plot line to the legend and then to the y axes in order to match plot and axis.

I've defined a style y axis style=<colour> that simultaneously sets the colour for y axis line style, y tick style, yticklabel style and ylabel style, so you can just call y axis style=red!75!black to set all the elements belonging to the y axis to that colour.

## One legend for both axes

\documentclass{article}

% UNITS
\usepackage{siunitx}

% 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=$Q/\si{\m\cubed\per\s}$,
ylabel=$H/\si{\m}$,
]
coordinates{
(0,68.6)
(0.0148,72)
(0.0295,68.6)
(0.0441,53.4)
(0.059,22.8)
}; \label{Hplot}
\end{axis}

\begin{axis}[
axis y line*=right,
axis x line=none,
ymin=0, ymax=100,
ylabel=$\eta/\si{\percent}$
]
\addlegendimage{/pgfplots/refstyle=Hplot}\addlegendentry{$H$}
coordinates{
(0,0)
(0.0148,48)
(0.0295,66)
(0.0441,66)
(0.059,45.0)
}; \addlegendentry{$\eta$}
\end{axis}

\end{tikzpicture}
\end{document}


## Plots directly labeled

\documentclass{article}

% UNITS
\usepackage{siunitx}

% GRAPHICS
\usepackage{tikz}
\usepackage{pgfplots}

\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=$Q/\si{\m\cubed\per\s}$,
ylabel=$H/\si{\m}$
]
coordinates{
(0,68.6)
(0.0148,72)
(0.0295,68.6)
(0.0441,53.4)
(0.059,22.8)
} node [pos=0.04,anchor=north, text=black] {$H$}; \label{Hplot}
\end{axis}

\begin{axis}[
axis y line*=right,
axis x line=none,
ymin=0, ymax=100,
ylabel=$\eta/\si{\percent}$
]
coordinates{
(0,0)
(0.0148,48)
(0.0295,66)
(0.0441,66)
(0.059,45.0)
} node [pos=0.9,anchor=south west, text=black] {$\eta$};
\end{axis}

\end{tikzpicture}
\end{document}


## Axis colours match plot colours

\documentclass{article}

% UNITS
\usepackage{siunitx}

% GRAPHICS
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
scale only axis,
scaled x ticks=base 10:3,
xmin=0, xmax=0.06,
y axis style/.style={
yticklabel style=#1,
ylabel style=#1,
y axis line style=#1,
ytick style=#1
}
}

\begin{axis}[
axis y line*=left,
y axis style=blue!75!black,
ymin=0, ymax=80,
xlabel=$Q/\si{\m\cubed\per\s}$,
ylabel=$H/\si{\m}$
]
coordinates{
(0,68.6)
(0.0148,72)
(0.0295,68.6)
(0.0441,53.4)
(0.059,22.8)
};
\end{axis}

\begin{axis}[
axis y line*=right,
axis x line=none,
ymin=0, ymax=100,
ylabel=$\eta/\si{\percent}$,
y axis style=red!75!black
]
coordinates{
(0,0)
(0.0148,48)
(0.0295,66)
(0.0441,66)
(0.059,45.0)
};
\end{axis}

\end{tikzpicture}
\end{document}

-
+1 Nice! I tried this approach with \label and \ref but failed. Do you think that is possible? –  cmhughes Jan 30 '12 at 16:51
I'm fine with doing things "by hand" as long as they work, and this worked just fine! Would it be possible to get it to work with \label and \ref. I noticed that it is position-dependent. I have another plot like this with an extra series in the first axis environment, and if I place the \addlegendimage{red,mark=*}{$\eta$} after that other series, the legend-image displays that series' style and not the one specified between the braces. The simple solution was to place the legend before that second \addplot in the first axis environment. –  Ricardo Jan 30 '12 at 20:27
@cmhughes: You could use \addlegendimage{legend image code/.code=\ref{<name>}}, which kind of works, but there are some issues with the alignment (adding a yshift=-0.5ex seems to work, but isn't very elegant). Instead of using \ref directly, you can access just the plot options associated with a labeled plot by calling the key /pgfplots/refstyle=<name>. I've edited my answer accordingly. –  Jake Jan 30 '12 at 22:42
@Ricardo: I've edited my answer to use the \label approach. You still need to be careful with the order you're calling \addlegendimage and \addplot, though: Each time you call one of those commands, the current plot style is added to a a list, and the instances of \addlegendentry just use the plot styles in the order they are found in that list. In other words, \addlegendentry doesn't care whether it's called directly after \addplot or \addlegendimage, it just goes by the list. –  Jake Jan 30 '12 at 22:51