曲线和 y 轴之间的填充

曲线和 y 轴之间的填充

以下代码填充了函数和 x 轴之间的区域,但编译器忽略了函数和 y 轴之间的填充。我在第二次填充时尝试了 range=,因为 domain= 对于 y 轴来说似乎很奇怪,但编译器不喜欢它。我遗漏了什么?

\documentclass[11pt,reqno]{amsbook} 
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots} \pgfplotsset{compat=1.18} \usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot[name path=f,domain=0:3]{x};
    \path[name path=a]{(axis cs:0,0) -- (axis cs:3,0)};
    \path[name path=b]{(axis cs:0,0) -- (axis cs:0,3)};
    \addplot [blue] fill between [of=f and a, soft clip={domain=1:2},];
    \addplot [green] fill between [of=f and b, soft clip={domain=1:2},];
\end{axis}
\end{tikzpicture}
\end{document}

答案1

@Raffaele Santoro 答案的一个小变化(+1):

%\documentclass[11pt,reqno]{amsbook} 
\documentclass[margin=3mm]{standalone}
\usepackage{amsmath, amssymb}
\usepackage{pgfplots} 
    \pgfplotsset{compat=1.18} 
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[scale=3]
\begin{axis}[
    width=4cm,
    axis lines=center,
    axis equal,
    axis on top,
    xmin=-0.2,  xmax=3.5,   
    ymin=-0.2,  ymax=3.5,
    xtick = {1,2,3},
    xlabel=$x$,
    ylabel=$y$,
    samples=100,
            ]
\addplot[domain=0:pi]   {x};
\addplot[name path=f,domain=1:2]   {x};
\draw[name path=a]  (0,1) -- (0,2);
\draw[name path=b]  (1,0) -- (2,0);
%
\addplot [blue!30]  fill between [of=a and f];
\addplot [green!30] fill between [of=b and f];
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

像这样:

在此处输入图片描述

代码:

\documentclass[11pt,reqno]{amsbook} 
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots} \pgfplotsset{compat=1.18} 
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[scale=3]
\begin{axis}[
            samples=100,
            ymin=-.2,ymax=3.5,
            width=4cm, height=4cm,
            xmin=-.2, xmax=3.8,
            axis x line=center,
            axis y line=center,
            xlabel=$x$,
            ylabel=$y$,
            %grid
            ]
    \addplot[name path=f,domain=0:pi]{x};
    \path[name path=a]{(axis cs:0,0) -- (axis cs:pi,0)};
    \path[name path=b]{(0,2) -- (2,2)};
    \addplot [blue] fill between [of=f and a, soft clip={domain=1:2},];
    \fill[green](0,1) rectangle (1,2);
    \addplot [green] fill between [of=b and f, soft clip={domain=1:2}];
\end{axis}
\end{tikzpicture}
\end{document}

另一个不太简单的例子(相同的序言):

\begin{tikzpicture}[scale=3]
\begin{axis}[
            samples=100,
            ymin=-.2,ymax=2,
            width=4cm, height=2.5cm,
            xmin=-.2, xmax=3.8,
            xtick={.524,1.5708,pi},
            ytick={.5,1,1.5},
            xticklabels={$\frac{\pi}{6}$,$\frac{\pi}{2}$,$\pi$},
            axis x line=center,
            axis y line=center,
            xlabel=$x$,
            ylabel=$y$,
            grid
            ]
    \addplot[name path=f,domain=0:pi,line width=1pt]{sin(deg(x))};
    \path[name path=a]{(axis cs:0,0) -- (axis cs:pi,0)};
    \path[name path=b]{(0,.5) -- (pi/6,.5)};
    \addplot [blue] fill between [of=f and a, soft clip={domain=1:pi},];
    \addplot [green] fill between [of=b and f, soft clip={domain=0:pi/6}];
\end{axis}
\end{tikzpicture}

输出:

在此处输入图片描述

相关内容