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 would like to draw several plots with lines and only a few marks. The markes are irregularely spaced, so I used a method alluded to in the pgfplots manual, section 4.8.5 (Legend appearance), page 159, every legend image post entry. The lines and the marks are created by different \addplot commands. The issue with this method is to have a legend showing both the lines and marks.

Here is an adaptation of the code provided in the pgfplots manual:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend image post style={mark=*}]
\addplot+[only marks,forget plot]coordinates {(0.5,-0.5) (1,-1) (1.5,-1.5)};
\addplot+[mark=none,smooth,domain=0:2]{-x};
\addlegendentry{Parabola}

\addplot+[only marks,forget plot]coordinates {(0.3,0.3) (0.45,0.45) (1.7,1.7)};
\addplot+[mark=none,smooth,domain=0:2]{x};
\addlegendentry{Parabola}
\end{axis}
\end{tikzpicture}
\end{document}

This code works when there is only one curve because one can specify the plot mark in the legend image post style key. However, do you know how this could be adapted to several plots with different plotmarks ?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

By providing the legend image post style to each \addplot command instead of to the axis, it only applies to each individual plot:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[only marks,forget plot]coordinates {(0.5,-0.5) (1,-1) (1.5,-1.5)};
\addplot+[mark=none,smooth,domain=0:2,legend image post style={mark=*}]{-x};
\addlegendentry{Parabola}

\addplot+[only marks,forget plot]coordinates {(0.3,0.3) (0.45,0.45) (1.7,1.7)};
\addplot+[mark=none,smooth,domain=0:2,legend image post style={mark=square*}]{x};
\addlegendentry{Parabola}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
It works indeed. Would there be a way to achieve the same result without having to manualy specify each mark ? Something like legend image post style=has mark so that the legend use the cycle list. –  Alfred M. Aug 31 '12 at 10:48
add comment

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.