3

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 ?

5

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}
1
  • 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. – M. Toya Aug 31 '12 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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