How can I plot two lines in the same style (using cycle lists) with pgfplots?

forget plot key is the usual way to do this but \addlegendentry{} does not choose which addplot command it comes after. You have to draw the to-be-legended(!?) plots consecutively at the beginning. So you can collate the plots such that you draw them 1-2-3,1-2-3, in terms of style as follows

\begin{tikzpicture}
\begin{axis}
% Plot using style 1
\addplot {ln(x)};\addlegendentry{Style 1}
\addplot {3*x};\addlegendentry{Style 2}

\pgfplotsset{cycle list shift=-2} % Goes two styles back
\addplot {0.5*x}; %Style 1
\addplot {2*x}; %Style 2
\end{axis}
\end{tikzpicture}

enter image description here


Never mind, I seem to have stumbled upon the answer myself in the pfdplots manual section 4.25 "Miscellaneous Options", specifically the forget plot option:

% Plot using style 1
\addplot+ [forget plot] coordinates {...};
\addplot+ coordinates {...};
\addlegendentry{Style 1}

% Plot using style 2
\addplot+ [forget plot] coordinates {...};
\addplot+ coordinates {...};
\addlegendentry{Style 2}

Tags:

Pgfplots