在 f(y) 和 f(x) 函数之间填充

在 f(y) 和 f(x) 函数之间填充

我想填充 f(x) 函数和 f(y) 函数之间的区域。这是我目前所得到的:

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

文本

我只想填充标有 D 的区域。我该怎么做呢?

答案1

尝试一个简单的\fill选项intersection segments

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

在此处输入图片描述

答案2

仅限 tikz:

在此处输入图片描述

代码:

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

相关内容