# How to fill 3 areas or more under a curve with different dashed colors?

I tried to use tikz and pgfplots packages in order to fill different areas under a curve with different colors.

The issue is in the script below. When I define the area's draw pattern for two first areas, it seems ok, as it gives me red and cyan dashed lines. I specify green as pattern color for the third one but the fill color remains cyan. Can anyone give me a hint to solve this. I want to have multiple filled areas with different filling colors.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}

\begin{axis}[
xmin=0,xmax=4,
xlabel={k'},
ymin=0,ymax=1,
axis on top,
legend style={legend cell align=right,legend plot pos=right}]

\path[name path=B] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

fill between[
of=A and B,
soft clip={domain=0:.25},
];

\addplot[pattern=flexible hatch,pattern color=cyan,draw=blue,hatch distance=5pt, hatch thickness=0.5pt]
fill between[
of=A and B,
soft clip={domain=0.25:0.5},
];

\addplot[pattern=flexible hatch,pattern color=green,draw=blue,hatch distance=5pt, hatch thickness=0.5pt]
fill between[
of=A and B,
soft clip={domain=0.5:0.9},
];

\end{axis}
\end{tikzpicture}
\end{document}


The problem lays in the definition of your custom pattern. With the link provided by Ross in the comments below this answer one can modify your definition which then works perfectly fine.

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

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
patterns,
pgfplots.fillbetween,
}
\pgfplotsset{compat=1.3}
% -------------------------------------------------------------------------
% the code bases was most likely borrowed from one of the following sources
%     <https://tex.stackexchange.com/a/119711/95441>
%     <https://tex.stackexchange.com/a/29367/95441>
% with some adaptions found in
%     <https://tex.stackexchange.com/a/60234/95441>
% it now works perfectly fine
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt,
hatch color/.store in=\hatchcolor,      % <-- added
}
\pgfdeclarepatternformonly[%
\hatchdistance,%
\hatchthickness,%
]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}
% -------------------------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=4,
xlabel={k'},
ymin=0,ymax=1,
axis on top,
]
color=red,
domain=0:4,
smooth,                             % <-- (changed from samples')
name path=A,
] {1/(x+1)};

\path [name path=B]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

draw,
pattern=flexible hatch,
hatch color=red,                    % <-- changed from pattern color'
] fill between [
of=A and B,
soft clip={domain=0:.25},
];

draw=blue,
pattern=flexible hatch,
hatch distance=5pt,
hatch thickness=0.5pt,
hatch color=cyan,
] fill between [
of=A and B,
soft clip={domain=0.25:0.5},
];

draw=blue,
pattern=flexible hatch,
hatch distance=5pt,
hatch thickness=0.5pt,
hatch color=green,
] fill between [
of=A and B,
soft clip={domain=0.5:0.9},
];
\end{axis}
\end{tikzpicture}
\end{document}


The solution to add \tikz@pattern@color to the list of variables. So replace

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}


by

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness,\tikz@pattern@color]{flexible hatch}


The reason is that when you use a pattern with variables for the first time, a new fixed instance of the pattern is declared. This is to make pattern drawing efficient. A new pattern is only declared, when the variables change. But you haven't put \tikz@pattern@color in the list of variables, so it does not notice that you changed the color and uses the parameters with which it has been declared before.