곡선과 y축 사이 채우기

곡선과 y축 사이 채우기

다음 코드는 함수와 x축 사이의 영역을 채우지만 컴파일러는 함수와 y축 사이의 채우기를 무시합니다. domain=이 y축에 이상해 보이기 때문에 두 번째 채우기에서 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}

산출:

여기에 이미지 설명을 입력하세요

관련 정보