Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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,
  ]
  \addplot[solid] {x^2};
  \addlegendentry{plot A};
\end{axis}
\begin{axis}[
  width=\linewidth,
  xmin=-5,xmax=5,
  ymin=0,ymax=25,
  axis y line*=right,
  axis x line*=bottom
  ]
  \addplot[dashed] {1.1*x^2};
  \addlegendentry{plot A};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The perfect outcome would be a legend entry like this:

_______
------- plot A
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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}

enter image description here

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,
  ]
  \addplot[solid] {x^2};
\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}
  ]
  \addplot[dashed] {1.1*x^2};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

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
  ]
  \addplot[dashed] {1.1*x^2};
  %\addlegendentry{plot A};
\end{axis}

gives:

enter image description here

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
  }
  ]
  \addlegendimage{draw}
  \addlegendentry{} % <= or use a \mbox{}
  \addlegendimage{dashed}
  \addlegendentry{plot A};
  \addplot[solid] {x^2};

\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,
  ]
  \addplot[dashed] {1.1*x^2};
\end{axis}
\end{tikzpicture}

\end{document}

The result:

enter image description here

share|improve this answer
    
Very nice, thanks. Is it also possible to center the with respect to the lines in the legend? –  bonanza May 27 at 16:04
    
Yes, you can, but you need some tricks that I would say Jesse's methods is better. However, I think center the text is not good and actually I would add a label for the solid plot to not confuse the reader. –  Claudio Fiandrino May 28 at 6:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.