2

I want to fill the area between an f(x) functions and an f(y) function. This is what I have so far:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz,bm}
\usepackage{pgfplots}
    \pgfplotsset{compat=newest}
    \usepgfplotslibrary{fillbetween}
    \usetikzlibrary{intersections}

\begin{document}

\pgfplotsset{
    standard/.style={
    axis line style = thick,
    grid = major,
    trig format=rad,
    enlargelimits,
    axis x line=middle,
    axis y line=middle,
    axis line style={latex-latex},
    enlarge x limits=0.15,
    enlarge y limits=0.15,
    every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
    every axis y label/.style={at={(current axis.above origin)},anchor=south east},
    }
}



\begin{center}
\begin{tikzpicture}
    \begin{axis}[standard,
    xtick={-4,-3,-2,...,6},
    ytick={-3,-2,...,4},
    xticklabels={-4, ,-2, ,0, ,2, ,4, ,6},
    yticklabels={,-2, , , ,2, ,4},
    xlabel={$x$},
    ylabel={$y$},
    samples=100,
    xmin=-3, xmax=5,
    ymin=-3, ymax=4]
        \addplot[name path=f, smooth, ultra thick, domain={-4:5}] ((x^2/2)-3,x);
            \node at (-2,3) {$y^2=2x+6$};
        \addplot[name path=g, smooth, ultra thick, domain={-3:6}]{x-1} node[right,pos=0.5] {$y=x-1$};

    \addplot[fill=black,fill opacity=0.1]fill between[of=f and g, soft clip={domain=-3:4}];

    \node at (0.5,1.5) {$\mathbf{D}$};
        
    \end{axis}
\end{tikzpicture}
\end{center}

\end{document}

text

I want to only fill the region labeled D. How would I go about doing this?

2 Answers 2

2

Try a simple \fill with the intersection segments option:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\pgfplotsset{
    standard/.style={
    axis line style = thick,
    grid = major,
    trig format=rad,
    enlargelimits,
    axis x line=middle,
    axis y line=middle,
    axis line style={latex-latex},
    enlarge x limits=0.15,
    enlarge y limits=0.15,
    every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
    every axis y label/.style={at={(current axis.above origin)},anchor=south east},
    }
}

\begin{center}
\begin{tikzpicture}
    \begin{axis}[standard,
    xtick={-4,-3,-2,...,6},
    ytick={-3,-2,...,4},
    xticklabels={-4, ,-2, ,0, ,2, ,4, ,6},
    yticklabels={,-2, , , ,2, ,4},
    xlabel={$x$},
    ylabel={$y$},
    samples=100,
    xmin=-3, xmax=5,
    ymin=-3, ymax=4]
        \addplot[name path=f, smooth, ultra thick, domain={-4:5}] ((x^2/2)-3,x);
        \node at (-2,3) {$y^2=2x+6$};
        \addplot[name path=g, smooth, ultra thick, domain={-3:6}]{x-1}  
            node[right,pos=0.5] {$y=x-1$};

        \fill[black, fill opacity=0.1, intersection segments={of=f and g, sequence={R2 -- L2}}] -- cycle;

        \node at (0.5,1.5) {$\mathbf{D}$};
    \end{axis}
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

1

Only with tikz:

enter image description here

Code:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage[margin=.5cm]{geometry}

\begin{document}
    \thispagestyle{empty}
    \begin{tikzpicture}
        \draw[gray!30,dashed] (-4,-4) grid (6,5);
        \draw[-latex] (-4.2,0)--(6.3,0) node[right] () {\footnotesize $x$};
        \foreach \i in {-4,...,-1,1,2,...,6}
        \draw (\i,-0)--(\i,-0.1) node[below] () {\footnotesize \i};
        \draw[-latex] (0,-4.2)--(0,5.3) node[above] () {\footnotesize $y$};
        \foreach \i in {-4,...,-1,1,2,...,5}
        \draw (0,\i)--(-.1,\i) node[left] () {\footnotesize \i};
        \draw (-.2,-.2) node () {\footnotesize $O$};
        %shading 1
        \foreach \x in {-3,-2.99,...,-1}{
            \pgfmathsetmacro{\y}{sqrt(2*\x+6)}
            \draw[gray!60,opacity=.2] (\x,\y)--(\x,-\y);}
        % shading 2
        \foreach \x in {-1,-0.99,...,5}{
            \pgfmathsetmacro{\y}{sqrt(2*\x+6)}
            \draw[gray!60,opacity=.2] (\x,\y)--(\x,\x-1);}  
        % plotting parabola
        \draw[blue,line width=2pt] plot[domain=-3:6,samples=500,smooth] (\x,{sqrt(2*\x+6)});
        \draw[blue,line width=2pt] plot[domain=-3:6,samples=500,smooth] (\x,{-sqrt(2*\x+6)});
        % plotting line
        \draw[magenta,line width=2pt] plot[domain=-3:6,smooth] (\x,{\x-1});
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .