# Pgfplots: let legend show two linestyles

I do have a double axis plot which which compares two different things (part 1 on the left axis in solid, part two on the right axis in dashed) for different paramters which are common for both plots. Now, since in general the legend is crowed (more than 6 entries) I would like to join some legend entries. Meaning 2 lines (one from left and one from right) which have the same parameter should also be joined in the legend. Meaning, It would be nice to have two linestyles (solid and dashed) combined in one legend entry.

Here is my MWE for double axis plot:

\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=left,
axis x line*=bottom,
]
\end{axis}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=right,
axis x line*=bottom
]
\end{axis}
\end{tikzpicture}
\end{document}


The perfect outcome would be a legend entry like this:

_______
------- plot A

-

An other approach is to use

 legend entries={plot A},
legend image code/.code={%
\draw[dashed] (0cm,-0.1cm) -- (0.5cm,-0.1cm);
\draw[solid]  (0cm, 0.1cm) -- (0.5cm, 0.1cm);
},
legend style={xshift=-0.5cm}


Code

\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=left,
axis x line*=bottom,
]
\end{axis}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=right,
axis x line*=bottom,
legend entries={plot A},
legend image code/.code={%
\draw[dashed] (0cm,-0.1cm) -- (0.5cm,-0.1cm);
\draw[solid] (0cm, 0.1cm) -- (0.5cm, 0.1cm);
},
legend style={xshift=-0.5cm}
]
\end{axis}
\end{tikzpicture}
\end{document}

-

Let us try to understand the problem.

You have 2 axis environments and each of them has a legend: no options are given for placement, hence we can really suppose that one legend covers the other one. Indeed, commenting the \addlegendentry in the second axis environment:

\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=right,
axis x line*=bottom
]
\end{axis}


gives:

with solid line which in turn is simply the legend being covered previously.

The solution, is usually to set up the legend with a combination of \addlegendentry and \addlegendimage: this should be done only in one axis environment. I prefer the first one usually. By selecting the first axis environment, it is often necessary to add set xtick and/or ytick and/or ztick to \empty otherwise you end up repeating ticks. In your case you just have to set xtick=\empty as in y axis they differ.

In conclusion:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=left,
axis x line*=bottom,
legend style={
xshift=-0.5cm, % <= avoid overlapping with the plots
}
]
\addlegendentry{} % <= or use a \mbox{}

\end{axis}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=right,
axis x line*=none, % <= no need
xtick=\empty,
]