6

This question resembles fill the area between two curves calculated by pgfplots, however I'm not able to use the stack plots solution.

I want to fill the left part of the lined area with one fill, and the right part of the lined area with another. (I'm sure my solution to making both lines has better alternatives. I'm just interested in the shapes of the lines, the function used for the curved line is not essential to keep.)

An MWE:

\documentclass{standalone}
\usepackage{tikz, pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=5]
    \addplot [domain=0:5,samples=100, pattern=north east lines] 
        {(x - 2)^2/(7*x) + 1} |- (current plot begin) ;
    \addplot [color=blue, thick, dashed] plot coordinates {(2,0) (2,5)} ;
    \end{axis}
\end{tikzpicture}
\end{document}

with the result:

enter image description here

2
  • 1
    I am currently writing pgfplots 1.10 which will come with builtin support for filled areas between plots/boundaries. Would you mind if I copy your example with a suitable solution to the pgfplots manual? – Christian Feuersänger Feb 27 '14 at 10:10
  • @ChristianFeuersänger Sure! Go ahead! – Holene Feb 27 '14 at 11:51
10

I think the easiest way to do this is to use two \addplot commands for the function, with different domains. To fill the area above the right side of the plot, you can append |- ({rel axis cs:0,1}-|{current plot begin}) after the \addplot command:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=5]
    \addplot [domain=0:2,samples=100, pattern=north east lines] 
        {(x - 2)^2/(7*x) + 1} |- (current plot begin) ;
        \addplot [domain=2:5,samples=100, pattern=north west lines] 
        {(x - 2)^2/(7*x) + 1} |- ({rel axis cs:0,1}-|{current plot begin}) ;
    \addplot [color=blue, thick, dashed] plot coordinates {(2,0) (2,5)} ;
    \end{axis}
\end{tikzpicture}
\end{document}
4

Version 1.10 of pgfplots has been released just recently, and it comes with a new solution for the problem to fill the area between plots.

Note that the old solution is still possible and still valid; this here is merely an update which might simplify the task. In order to keep the knowledge base of this site up-to-date, I present a solution based on the new fillbetween library here:

enter image description here

\documentclass{standalone}
\usepackage{tikz, pgfplots}

\usetikzlibrary{patterns}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=5,ymin=0,ymax=5]
    \addplot [name path=f,domain=0:5,samples=100] 
        {(x - 2)^2/(7*x) + 1};
    \addplot [name path=border,color=blue, thick, dashed] plot coordinates {(2,0) (2,5) } ;

    \fill[intersection segments={of=f and border,sequence={A0 -- B1}},
        pattern=north east lines,
    ]
    -- cycle;

    \fill[intersection segments={of=f and border,sequence={B1[reverse] -- A1}},
        pattern=north west lines,
    ]
    -- (rel axis cs:1,1) -- cycle;

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

This solution assigns the name f to the function as such, and it assigns the name border to the blue border line.

Then, it generates two \fill instructions, both of which make use of the new feature intersection segments: it allows to combine intersection segments of two input paths.

The first \fill statement is for the left side of the border: it starts with the segment A0 which means the 0th segment of the first argument in f and border, i.e. the first segment of f. Then, it connects this start point with B1 which is the second (1st) path segment of the second argument in f and border, i.e. the upper part of the blue line. Finally, it appends --cycle which connects with the start point.

The second \fill statement is for the right side of the border: it starts with the segment B1[reverse] which means the second (1st) part of the border line (B is always the second argument in of=<first> and <second>) and the option [reverse] means to reverse this path. Then, it connects this first segment with A1 which is the second (1st) segment of the function f. Then, it connects this part with -- (rel axis cs:1,1) which is x=100% and y=100% (i.e. the upper right corner).

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.