曲線とy軸の間を塗りつぶす

曲線とy軸の間を塗りつぶす

次のコードは関数と x 軸の間の領域を塗りつぶしますが、コンパイラは関数と y 軸の間の塗りつぶしを無視します。domain= は y 軸に対して奇妙に思えるので、2 番目の塗りつぶしで range= を試しましたが、コンパイラはそれを好みませんでした。何が足りないのでしょうか?

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

出力:

ここに画像の説明を入力してください

関連情報