Füllung zwischen Kurve und Y-Achse

Füllung zwischen Kurve und Y-Achse

Der folgende Code füllt den Bereich zwischen der Funktion und der x-Achse, aber der Compiler ignoriert die Füllung zwischen der Funktion und der y-Achse. Ich habe range= für die zweite Füllung ausprobiert, weil domain= für die y-Achse seltsam erscheint, aber dem Compiler gefiel das nicht. Was übersehe ich?

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

Antwort1

Eine kleine Variation der Antwort von @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}

Bildbeschreibung hier eingeben

Antwort2

So was:

Bildbeschreibung hier eingeben

Code:

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

Ein anderes, weniger einfaches Beispiel (dieselbe Präambel):

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

Ausgabe:

Bildbeschreibung hier eingeben

verwandte Informationen