Skip a style for a bar in a bar plot

Let’s say you add five data series to a bar plot and they would get the colors blue – red – brown – gray – purple. Now suppose you have another plot with only four data series, but you would like them to have the colors blue – red – gray – purple, because they are similar to the series 1, 2, 4 and 5 in the first plot. You also don’t want to change the order. What can you do?

The style (colors, markers, etc) for a dataseries are determined by the cycle list in pgfplots. This is a series of style definitions that are applied to your data series one after the other. You can of course define one cycle list for each of the plots and assign the colors the way you want:

\pgfplotscreateplotcyclelist{my five bars}{%
solid,fill,blue, \\%
solid,fill,red, \\%
solid,fill,brown, \\%
solid,fill,gray, \\%
solid,fill,purple, \\%
}
\pgfplotscreateplotcyclelist{my four bars}{%
solid,fill,blue, \\%
solid,fill,red, \\%
solid,fill,gray, \\%
solid,fill,purple, \\%
}

\begin{tikzpicture}
\begin{axis}[cycle list name=my five bars,...]
... add the five data series ...
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[cycle list name=my four bars,...]
... add the four data series ...
\end{axis}
\end{tikzpicture}


But you always need to remember to change both versions. Fortunately there is an easier way! You can shift the index of the cycle list:

\begin{axis}[cycle list name=my five bars,...]
... add first two data series ...
\pgfplotsset{cycle list shift=1} % Skips one style
... add the other two data series ...
\end{axis}
\end{tikzpicture}


Done!

Plot legend in figure caption

If you have a plot and add names to the data series with \addlegendentry{}, a legend is added to the plot that specifies the names for the lines or bars. This is an example plot from my thesis:

\begin{figure}
\begin{tikzpicture}
\begin{axis}[myplot]
\addplot plot coordinates {(1, 0.17) (2, 0.13) (3, 0.09) (4, 0.06) (5, 0.01) (6, 0.01)};
\addlegendentry{System1} % for legend
\addplot plot coordinates {(1, 0.13) (2, 0.16) (3, 0.16) (4, 0.14) (5, 0.14) (6, 0.13)};
\addlegendentry{System2} % for legend
\end{axis}
\end{tikzpicture}
\caption{Results (Precision) at different $k$ for the two systems}
\end{figure}


You can influence the placement and appearance of the legend and it looks really professional. Most of the time that will be exactly what you want. But not always. I wanted the legend to be embedded in the text of the caption (long story why). And this is possible and even quite simple! You just need to define a label for the data series and when you refer to that label, a picture of the line and marker is drawn as the reference.

\begin{figure}
\begin{tikzpicture}
\begin{axis}[myplot]
\addplot plot coordinates {(1, 0.17) (2, 0.13) (3, 0.09) (4, 0.06) (5, 0.01) (6, 0.01)};
\label{tikz:System1}
%\addlegendentry{System1} % remove to get rid of legend
\addplot plot coordinates {(1, 0.13) (2, 0.16) (3, 0.16) (4, 0.14) (5, 0.14) (6, 0.13)};
\label{tikz:System2}
%\addlegendentry{System2}
\end{axis}
\end{tikzpicture}
\caption{Results (Precision) at different $k$ for the two systems \ref{tikz:System1} System 1 and \ref{tikz:System2} System 2}
\end{figure}


Pgfplots is cool!

Hat tip: Stackexchange (the question is about something else)