3

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}]

\addplot[name path=A,color=red,domain=0:4,samples=100] {1/(x+1)};


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

\addplot+[draw,pattern=flexible hatch,pattern color=red]
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} 
3

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
        hatch color=black,                      % <-- added
    }
    \pgfdeclarepatternformonly[%
        \hatchdistance,%
        \hatchthickness,%
        \hatchcolor%                            % <-- added
            ]{flexible hatch}
    {\pgfqpoint{0pt}{0pt}}
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfsetstrokecolor{\hatchcolor}%        % <-- added
        \pgfusepath{stroke}
    }
    % -------------------------------------------------------------------------
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=0,xmax=4,
        xlabel={k'},
        ymin=0,ymax=1,
        axis on top,
    ]
        \addplot [
            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);

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

        \addplot [
            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},
        ];

        \addplot [
            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}

image showing the result of above code

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.