4

I would like to ask a question on how to shaded the area between two curves.

First of all the following is the code

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0mm}
\pgfplotsset{every axis/.append style={
        axis x line=middle,    % put the x axis in the middle
        axis y line=middle,    % put the y axis in the middle
        axis line style={<->}, % arrows on the axis
        xlabel={$x$},          % default put x on x-axis
        ylabel={$y=\sqrt{-x+1}$},          % default put y on y-axis
        ticks=none
    }}
    % arrows as stealth fighters
    \tikzset{>=stealth}
    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        xmin=-4,xmax=2,
        ymin=-1,ymax=3,
        ]
        \plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
        \plot[name path=B,thick,samples=100,domain=-4:1] {2};
        \addplot[fill=gray,opacity=.4,domain=0:1,samples=50] {sqrt(-1*x+1)}\closedcycle;
        %\addplot[fill=gray, domain=-3:0,opacity=0.4] fill between[of=A and B];
        \addplot+[mark=none,fill=gray,draw=black,domain=-3:0,opacity=0.4] {max(2-sqrt(-x+1),sqrt(-x+1))} \closedcycle;
        \node [below] at (axis cs: 0.2,0) {$O$};
        \draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
        \node [below] at (axis cs: -3,0) {$-3$};
        \node [below] at (axis cs: 1,0) {$1$};
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

So the above code generated the following plot. I actually want the shaded area to be between the horizontal line(y=2) and the curve(y=\sqrt{-x+1}) on x=[-3,0], whereas it is just shading the area under the curve and the x-axis.

How do you achieve this?

output

5
  • Welcome to TeX.SX! Please complete your example in order to make it compilable. "But I am having trouble with this" is not telling us so much. What have you tried, where are you stuck, what problems are you getting?
    – LaRiFaRi
    Aug 13, 2015 at 9:28
  • Sorry, it's my first time so I'm fiddling with things. Forgive me if this is a bit frustrating. Is what I am asking a bit clearer now?
    – Will Kim
    Aug 13, 2015 at 9:38
  • No need for sorry. Much better like this.
    – LaRiFaRi
    Aug 13, 2015 at 11:19
  • See here for Venn diagrams.
    – user10274
    Aug 31, 2015 at 10:08
  • You have edited this question in such a way that it is now entirely different and existing answers now make no sense. You should roll this question back, accept an answer if appropriate and ask a new question if required.
    – cfr
    Sep 1, 2015 at 0:22

3 Answers 3

4

enter image description here

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0mm}
\pgfplotsset{every axis/.append style={
        axis x line=middle,    % put the x axis in the middle
        axis y line=middle,    % put the y axis in the middle
        axis line style={<->}, % arrows on the axis
        xlabel={$x$},          % default put x on x-axis
        ylabel={$y=\sqrt{-x+1}$},          % default put y on y-axis
        ticks=none
    }}
    % arrows as stealth fighters
    \tikzset{>=stealth}
    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        xmin=-4,xmax=2,
        ymin=-1,ymax=3,
        ]
        \plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
        \plot[name path=B,thick,samples=100,domain=-4:1] {2};
        \addplot[fill=gray,opacity=0.4] fill between[of=A and B,split,soft clip={domain=-3:0},every segment no 0/.style={white},];
        \node [below] at (axis cs: 0.2,0) {$O$};
        \draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
        \node [below] at (axis cs: -3,0) {$-3$};
        \node [below] at (axis cs: 1,0) {$1$};
        \node [above right] at (axis cs: 0,2) {$2$};
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

To fill an area between curves, we could style the regions individually using every segment no and apply a soft clip if necessary as in your case [-3,0].

0
1

Something like this?

\addplot fill between[
  of = func and line,
  soft clip={domain=-3:0},
  every even segment/.style  = {gray,opacity=.4}
];

enter image description here

I cleaned up your code a bit. Here is a complete example:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{every axis/.append style={
        axis x line=middle,    % put the x axis in the middle
        axis y line=middle,    % put the y axis in the middle
        axis line style={<->}, % arrows on the axis
    }}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},              % default put x on x-axis
    ylabel={$y=\sqrt{-x+1}$},  % default put y on y-axis
    xtick={-3,0,1},
    ytick={2},
    samples=100,
    domain=-4:1,
    xmin=-4,xmax=2,
    ymin=-1,ymax=3,
  ]
  \addplot[name path=func, black, thick, mark=none, ] {sqrt(-x+1)};
  \addplot[name path=line,thick] {2};
  \addplot fill between[
    of = func and line,
    soft clip={domain=-3:0},
    every even segment/.style  = {gray,opacity=.4}
  ];
  \draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
\end{axis}
\end{tikzpicture}
\end{document}

BTW:

You don't need nodes to place axis tick labels, use the xtick and xtick options instead.

0
1

You can use soft clip to restrict the filling to the domain x=[-3,0]:

\addplot[fill=gray,opacity=0.4] fill between [of=A and B, soft clip={domain=-3:0}];

Thus the following code

\documentclass{article}
\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween}

\pgfplotsset{every axis/.append style={
  axis x line=middle,    % put the x axis in the middle
  axis y line=middle,    % put the y axis in the middle
  axis line style={<->}, % arrows on the axis
  xlabel={$x$},          % default put x on x-axis
  ylabel={$y=\sqrt{-x+1}$},          % default put y on y-axis
  ticks=none
}}

% arrows as stealth fighters
\tikzset{>=stealth}

\begin{document}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      xmin=-4,xmax=2,
      ymin=-1,ymax=3,
      ]
      \plot[name path=A, thick,samples=100,domain=-4:1] {sqrt(-1*x+1)};
      \plot[name path=B,thick,samples=100,domain=-4:1] {2};
      \addplot[fill=gray,opacity=0.4] fill between [of=A and B, soft clip={domain=-3:0}];
      \node [below] at (axis cs: 0.2,0) {$O$};
      \draw[thick,dashed,brown] (axis cs:-3,0) -- (axis cs:-3,2);
      \node [below] at (axis cs: -3,0) {$-3$};
      \node [below] at (axis cs: 1,0) {$1$};
    \end{axis}
  \end{tikzpicture}
\end{center}

\end{document}

gives the output

Output

0

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.