[Tex/LaTex] Color cycle and forget plot

colorlegendpgfplots

I use cycle list name=exotic as coloring for my plots. I am looking for a way, either to use a number referring to a color (index from the exotic list), or a way to get the color of a labelled plot.

Here are an example of what I tried :

    \pgfplotsset{cycle list name=exotic}
    \begin{tikzpicture}
    \begin{axis}[enlarge x limits=false]
        \addplot+[thick, no marks, domain=0:360, samples=50, forget plot]  {sin(x)};
        \addplot+[thick, no marks, domain=0:360, samples=50]  {cos(x)};
        \legend{plot 1, plot 2}
    \end{axis}
    \end{tikzpicture}

But of course, the first plot is "forgotten" and therefore not taken into account for the color code.
(Accessing the color of a labelled plot would probably be useful, even if it exist a simpler solution.)

Best Answer

I'll give here a general example how you can do this. The trick is to first define a colormap and then to convert this colormap to a cycle list. For other stuff you can then access the colors of the colormap with the new feature index of colormap which was introduced in PGFPlots v1.13.

For more details have a look at the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.11,
        %
        % define a colormap that contains the colors you
        % want to use in the `cycle list'
        colormap={my colormap}{
            color=(red)
            color=(blue!50)
            color=(green!50!black)
            color=(orange)
        },
        % define some other `cycle list's to later combine them if you need
        % I will use here some predefined ones for simplicity
    }
    % now create a `cycle list' from that colors
    \pgfplotscreateplotcyclelist{my color cycle list}{
        [of colormap=my colormap]
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % some options to better visualize the result
            stack plots=y,
            every axis plot/.style={
                line width=2pt,
            },
            %
            % define here the `cycle list' you need
            cycle multiindex* list={
                my color cycle list
                    \nextlist
                [3 of]mark list
                    \nextlist
                linestyles
                    \nextlist
            },
        ]
            % add some plots
            \addplot coordinates {(0,1) (0.5,1) (1,1)};
            \addplot coordinates {(0,1) (0.5,1) (1,1)};
            \addplot coordinates {(0,1) (0.5,1) (1,1)};
            \addplot coordinates {(0,1) (0.5,1) (1,1)};

            % if you want to use the color of the 2nd plot you can do this via
            % (you need index 1 here, because the index starts at 0)
            \node [index of colormap=1,anchor=south]
                at (0.25,2) {I am the 2nd color};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code