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 have the following code

\documentclass[tikz,convert={size=640}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries={simulation sample 1,
                measurement sample 1,
                simulation sample 2,
                measurement sample 2},
legend pos=outer north east]
\addplot[mark=o,color=blue] coordinates {
  (0,0)
  (1,1)
  (2,2)
};
\addplot[mark=o,color=green] coordinates {
  (0,1)
  (1,1.5)
  (2,1.8)
};
\addplot[mark=square,color=blue] coordinates {
  (3,0.5)
  (4.5,1)
  (5,2)
};
\addplot[mark=square,color=green] coordinates {
  (3,0)
  (4,1)
  (5,2.2)
};
\end{axis}
\end{tikzpicture}
\end{document}

which produces this image:

enter image description here

But now I want to group my legend entries and only have an entry with line color blue to indicate the simulations and one with line color green for measurements and then one for the circle marker for the sample 1 and one for the square marker to indicate the sample 2. How do I achieve this? Can I insert a ghost plot which is only visible in the entries, or can I use the \addlegendimage command to adjust the legend image?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Here's slightly different version of Martin H's excellent answer that works without the \addlegendentry commands and uses the legend entries key instead (I find that to be more readable). The legend entries are assigned to the \addplot and \addlegendimage commands in the order they're given, so if you move the \addlegendimages before the \addplots (or if you add forget plot to the \addplot options), you can use the legend entries key:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries={simulation,
                measurement,
                sample 1,
                sample 2},
legend pos=outer north east]
\addlegendimage{no markers,green}
\addlegendimage{no markers,blue}
\addlegendimage{only marks, mark=o}
\addlegendimage{only marks, mark=square}
\addplot[mark=o,color=blue] coordinates {
  (0,0)
  (1,1)
  (2,2)
};
\addplot[mark=o,color=green] coordinates {
  (0,1)
  (1,1.5)
  (2,1.8)
};
\addplot[mark=square,color=blue] coordinates {
  (3,0.5)
  (4.5,1)
  (5,2)
};
\addplot[mark=square,color=green] coordinates {
  (3,0)
  (4,1)
  (5,2.2)
};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

You should be able to get the first part of your custom legend like so

\addlegendimage{line legend,green}
\addlegendentry{Simulation}
\addlegendimage{line legend,blue}
\addlegendentry{Measurement}

obviously for that to work you should comment the legend entries= that you have in your code now. Just add the above code after you \addplot commands.

I am not sure how to get the matching plot marks

EDIT

With the comment from Jake this seems to work

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}




\begin{document}
\begin{tikzpicture}
    \begin{axis}
    \addplot+[forget plot] coordinates {(1,1) (2,2)};
\addlegendimage{line legend,green} % or mark=none?
\addlegendentry{Simulation}
\addlegendimage{line legend,blue}
\addlegendentry{Measurement}
\addlegendimage{green,mark=square}
\addlegendentry{Set 1}
\addlegendimage{blue,mark=o}
\addlegendentry{Set 2}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
2  
You can pass the same options to \addlegendimage as you would to \addplot, so to get the circle and square marks, you could use \addlegendimage{only marks, mark=o} \addlegendimage{only marks, mark=square}. Also, if you put the \addlegendimage commands before your \addplots, and/or you add forget plot to the \addplots, you can still use the legend entries key and don't have to use \addlegendentry. –  Jake May 11 '13 at 17:52
    
I didn't see that in the manual, must have missed it, but thanks a lot for the clarification! –  Reza May 13 '13 at 6:11

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.