46

Is it possible to combine a TikZ pattern with a background of a chosen color?

\addplot[black,fill=yellow,pattern=north east lines] ...

yields bars which have black stripes on white (transparent?) background, whereas

\addplot[black,pattern=north east lines,fill=yellow] ...

yields bars which are plain yellow with not stripes at all. Any chance of getting tiger-like bars (black stripes on yellow) without defining an inherently-colored pattern by hand?

Andreas
  • 463

3 Answers3

55

You can use a postaction to draw the pattern after the yellow background.

tiger bar plot

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}


\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar, samples=10, enlarge y limits=upper, ymin=0]
\addplot +[
    black,
    fill=yellow,
    postaction={
        pattern=north east lines
    }
]{rnd};
\end{axis}

\end{tikzpicture}
\end{document}

In older versions of PGFfplots, you needed to use every path to apply the pattern.

In order to apply the postaction, you can use every path/.style={postaction={...}}. This requires that you clear the postaction, e.g. using the approach given in Applying a postaction to every path in TikZ, as otherwise you get an infinite recursion.

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

\makeatletter
\tikzset{nomorepostaction/.code=\let\tikz@postactions\pgfutil@empty}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar, samples=10]
\addplot +[
    black,
    fill=yellow,
    every path/.style={
        postaction={
            nomorepostaction,
            pattern=north east lines
        }
    }
]{rnd};
\end{axis}

\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • Works like a charm. :-) – Andreas Aug 08 '11 at 07:29
  • This doesn't work anymore with the latest version? – Mobius Pizza Jan 23 '12 at 10:49
  • @MobiusPizza: It does for me (with version 1.5.1). I forgot the \end{document} in my example, so maybe that's what caused the failure for you? – Jake Jan 23 '12 at 11:04
  • @Jake I can confirm that indeed the new version does not render the patterns. – percusse Apr 20 '12 at 19:12
  • 1
    @percusse: You're right, the new version doesn't seem to need the every path approach any more. I've edited my answer. – Jake Apr 20 '12 at 19:57
  • Unfortunately, both examples do not work for me (pgfplots 1.5.1). – luator Feb 29 '16 at 13:18
  • @luator: You might want to consider updating your PGFPlots installation, 1.5.1 is pretty old now. If that doesn't work, please open a new question that describes the error in a bit more detail – Jake Feb 29 '16 at 13:20
  • @Jake: It compiles without error, just the north east lines will not appear. I agree that an update would be best in general, but I have to use Ubuntu 12.04 right now and would rather avoid messing with the package versions. – luator Feb 29 '16 at 13:32
  • Thanks to the comment of cacamailg to this answer, I found out that the problem seems to be pdflatex. When I compile with latex it works as expected. This issue is also explicitly addressed in this question. – luator Feb 29 '16 at 13:48
2

I think the answer is yes: by two separate path instructions with the same path: one without pattern but with fill color and one with the chosen pattern.

In terms of the \addplot command of pgfplots: two separate \addplot commands, one with \addplot[fill,forget plot,...] and one with the pattern. The forget plot tells pgfplots to avoid legend entries for the plot.

If this appears to be unsuitable, you may want to look into decorations - perhaps they support such a thing.

2

another simple way is to use pattern

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepgfplotslibrary{groupplots}

\begin{document} \begin{figure} \centering \begin{tikzpicture} \begin{groupplot}[ legend columns=-1, legend entries={{\color{red}{\tiny Random}},{\color{blue}{\tiny ++Cost}},{\color{black}{\tiny ++FTE}},{\color{green}{\tiny ++Resources}},{\color{orange}{\tiny Hold All}}}, legend to name=CombinedLegendBar, area legend, group style={ group size=1 by 1, xlabels at=edge bottom, ylabels at=edge left, xticklabels at=edge bottom}] \nextgroupplot[bar width=17pt, xticklabels=\empty] \addplot[ybar, pattern=horizontal lines] coordinates { (1, 12)}; \addplot[ybar, pattern=vertical lines] coordinates { (2, 3)}; \addplot[ybar, pattern=grid] coordinates { (3, 3)}; \addplot[ybar, pattern=dots] coordinates { (4, 2)}; \addplot[ybar, pattern=north east lines] coordinates { (5, 2)};

\end{groupplot} \end{tikzpicture} \ref{CombinedLegendBar} \caption{Triage++ Performance} \label{PlusPlusCombinedBar} \end{figure} \end{document}

Kasra
  • 121