1

This question is related to this other question. Fill between fails to fill the correct region in this MWE.

\documentclass{article}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween} 
\usetikzlibrary{patterns}

\pgfplotsset{
    every tick/.append style={thin},
    axis x line=center,
    axis y line=center,
    xtick={-100,-99,...,100},
    ytick={-100,-99,...,100},
    xlabel=$x$,
    ylabel=$y$,
    xticklabel=\empty,
    yticklabel=\empty,
    xlabel style={at=(current axis.right of origin), anchor=west}, 
    ylabel style={at=(current axis.above origin), anchor=south}, 
    every tick label/.append style={font=\scriptsize},
    axis equal image,
    axis line style={black!66!white},
    every axis plot/.append style={smooth,thick,mark=none,font=\small},
    every node/.style={label font=\small},
    every axis legend/.append style={at={(1,1)},anchor=south east,empty legend},
    }

\pgfplotsset{rellenoa/.style={pattern=north west lines,pattern color=green,}}
\pgfplotsset{rellenob/.style={pattern=north east lines,pattern color=green,}}
\pgfplotsset{rellenoc/.style={pattern=horizontal lines,pattern color=green,}}

\begin{document}

\begin{center}
\begin{tikzpicture}
    \def\xmi{-4.5} \def\xma{6.5} \def\ymi{-4.9} \def\yma{12.5} 
    \def\coa{-2} \def\cob{0.5} \def\coc{4} 
\begin{axis}[
        %scale=0.7, % this works
width=0.6\textwidth,xmin=\xmi,xmax=\xma,ymin=\ymi,ymax=\yma,y=1.5cm,
declare function={curvaa(\t)=-2*\t+4;},
declare function={curvab(\t)=2*\t+2;},
declare function={caminoa(\t)=-2*\t+1;},
declare function={caminob(\t)=2*\t-1;},]
%Lines
\addplot[domain=\xmi-1:\cob,samples=2,blue,name path=crva]{curvaa(x)};
\addplot[domain=\cob:\xma+1,samples=2,blue,name path=crvb]{curvab(x)};
\addplot[domain=\xmi-1:\cob,samples=2,purple]{caminoa(x)};
\addplot[domain=\cob:\xma+1,samples=2,purple]{caminob(x)};
%Points
\node[blue](X1t) at (axis cs:{\coa,caminoa(\coa)}) {};
\node[blue](X2t) at (axis cs:{\coa,curvaa(\coa)}) {};
    \node[](X2m) at (axis cs:{\coa,curvaa(\cob)}) {};
    \node[](X2b) at (axis cs:\coa,0) {};
\node[blue](X3t) at (axis cs:{\cob,curvaa(\cob)}) {};
    \node[](X3b) at (axis cs:\cob,0) {};
\node[blue](X4t) at (axis cs:{\coc,curvab(\coc)}) {};
    \node[](X4m) at (axis cs:{\coc,curvab(\cob)}) {};
    \node[](X4b) at (axis cs:\coc,0) {};
\node[blue](X5t) at (axis cs:{\coc,caminob(\coc)}) {};
%Barriers
\draw[/pgfplots,red,dashed,] (X1t.center) -- (X2b.center);
\draw[/pgfplots,red,dashed,] (X5t.center) -- (X4b.center);
\draw[/pgfplots,red,dashed,] (X2t.center) -- (X2b.center);
\draw[/pgfplots,red,dashed,] (X4t.center) -- (X4b.center);
    \path[name path=bed] (X2b.center)-- (X4b.center);
\draw[/pgfplots,red,dashed,name path=mid] (X2m.center) -- (X4m.center);
%Fills
\addplot[rellenoa] fill between [of=mid and crva,soft clip={domain=\coa:\cob},];
\addplot[rellenob] fill between [of=crvb and mid,soft clip={domain=\cob:\coc},];
\addplot[rellenoc] fill between [of=bed and mid,soft clip={domain=\coa:\coc},];
%Legends
%\legend{\empty,\empty,\empty,\empty,$A_1$,$A_2$,$A_3$}
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

I've encountered this bug (maybe?) before, and I've dealt with it by playing around with the value of y, but nothing seems to work with this one.

  • I guess that the domain of the soft clips is not chosen appropriately. More precisely, you may want to fill the region between certain intersection segments. However, from your question it is not clear which regions you want to fill. Please specify them. – user121799 Oct 20 '18 at 21:59
  • I'm sorry, I should've been more specific. I wish to fill three areas. The big rectangle (horizontal pattern), and the two big triangles (with blue hypotenuses) above the rectangle. The fill in the right triangle exceeds the triangle. – The Fourth Man Oct 21 '18 at 0:18
  • OK, I see. I guess you slightly misinterpret soft clip, but I can see how this might be confusing. The left triangle works by accident. I added some more explanations to my answer, where I cite the relevant part of the manual. – user121799 Oct 21 '18 at 1:16
2

IMHO, there is no bug, and the fills are precisely as one would expect them to be from the manual. I am referring to the statement starting in the third line of p. 104

The soft clip key restricts the filled segments to the bounding rectangle defined by some path ⟨lower corner⟩ rectangle ⟨upper corner⟩ where the two arguments are the lower and upper corner of an invisible rectangular bounding box.

Continuing to read on p. 104 suggests the code

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween} 
\usetikzlibrary{patterns}

\pgfplotsset{compat=1.16,
    every tick/.append style={thin},
    axis x line=center,
    axis y line=center,
    xtick={-100,-99,...,100},
    ytick={-100,-99,...,100},
    xlabel=$x$,
    ylabel=$y$,
    xticklabel=\empty,
    yticklabel=\empty,
    xlabel style={at=(current axis.right of origin), anchor=west}, 
    ylabel style={at=(current axis.above origin), anchor=south}, 
    every tick label/.append style={font=\scriptsize},
    axis equal image,
    axis line style={black!66!white},
    every axis plot/.append style={smooth,thick,mark=none,font=\small},
    every node/.style={label font=\small},
    every axis legend/.append style={at={(1,1)},anchor=south east,empty legend},
    }

\pgfplotsset{rellenoa/.style={pattern=north west lines,pattern color=green,}}
\pgfplotsset{rellenob/.style={pattern=north east lines,pattern color=green,}}
\pgfplotsset{rellenoc/.style={pattern=horizontal lines,pattern color=green,}}

\begin{document}

\begin{center}
\begin{tikzpicture}
    \def\xmi{-4.5} \def\xma{6.5} \def\ymi{-4.9} \def\yma{12.5} 
    \def\coa{-2} \def\cob{0.5} \def\coc{4} 
\begin{axis}[
        %scale=0.7, % this works
width=0.6\textwidth,xmin=\xmi,xmax=\xma,ymin=\ymi,ymax=\yma,y=1.5cm,
declare function={curvaa(\t)=-2*\t+4;},
declare function={curvab(\t)=2*\t+2;},
declare function={caminoa(\t)=-2*\t+1;},
declare function={caminob(\t)=2*\t-1;},]
%Lines
\addplot[domain=\xmi-1:\cob,samples=2,blue,name path=crva]{curvaa(x)};
\addplot[domain=\cob:\xma+1,samples=2,blue,name path=crvb]{curvab(x)};
\addplot[domain=\xmi-1:\cob,samples=2,purple]{caminoa(x)};
\addplot[domain=\cob:\xma+1,samples=2,purple]{caminob(x)};
%Points
\coordinate(X1t) at (axis cs:{\coa,caminoa(\coa)});
\coordinate(X2t) at (axis cs:{\coa,curvaa(\coa)});
\coordinate(X2m) at (axis cs:{\coa,curvaa(\cob)});
\coordinate(X2b) at (axis cs:\coa,0);
\coordinate(X3t) at (axis cs:{\cob,curvaa(\cob)});
\coordinate(X3b) at (axis cs:\cob,0);
\coordinate(X4t) at (axis cs:{\coc,curvab(\coc)});
\coordinate(X4m) at (axis cs:{\coc,curvab(\cob)});
\coordinate(X4b) at (axis cs:\coc,0);
\coordinate(X5t) at (axis cs:{\coc,caminob(\coc)});
%Barriers
% \draw[red,dashed] (X1t.center) -- (X2b.center);
% \draw[red,dashed] (X5t.center) -- (X4b.center);
\draw[red,dashed] (X2t) -- (X2b);
\draw[red,dashed] (X4t) -- (X4b);
\path[name path=bed] (X2b)-- (X4b);
\draw[red,dashed,name path=mid] (X2m) -- (X4m);
%Fills
\addplot[rellenoa] fill between [of=mid and crva,soft clip={domain=\coa:\cob}];
\def\clippath{
        (X3t) rectangle (X4t)
    }
\addplot[rellenob] fill between [of=mid and crvb,soft clip={\clippath}];
\addplot[rellenoc] fill between [of=bed and mid,soft clip={domain=\coa:\coc}];
%Legends
%\legend{\empty,\empty,\empty,\empty,$A_1$,$A_2$,$A_3$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

I have a couple of remarks:

  1. I cleaned up your code. Many of your nodes can be more conveniently defined as coordinates, in which case you can drop many {} and .center.
  2. If you were to set compat to something recent (1.11 or higher), you could get rid of the axis cs:.
  3. Some of your paths were redundant, I commented them out.
  4. In \draw[/pgfplots,red,dashed,]... /pgfplots has AFAIK no effect.
  • Thank you, this works. Interesting though, I had tried a very similar solution but with soft clip={(X3t) rectangle (X4t)}, or something similar, and it failed. You defined that as an additional path and now it works. Also I use nodes because sometimes I need to style and make some of them visible, which I edited out for the MWE. – The Fourth Man Oct 21 '18 at 3:28
  • 1
    @TheFourthMan Yes, one could write a book on the expansion issues of pgfplots. And yes, these were all remarks, of course you may have a very good reason to go the way you go. On the other hand, this answer is meant to help everyone who may encounter the same problem, which is why I tried to simplify the code a bit. – user121799 Oct 21 '18 at 3:39

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.