两个地块之间的填充问题

两个地块之间的填充问题

我希望能够绘制两条曲线,如下所示:

在此处输入图片描述

这是我目前拥有的代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[ymin = 0, axis x line = bottom, axis y line=middle, legend pos=outer north east]
\addplot [name path=A, domain=-5:5,cyan, thick]{x^2 + 2*x + 2};
\addplot [name path=B, domain=0:10,orange,thick]{2*x + 2};
\legend{$f(x)$, $g(x)$}
\addplot[blue!20] fill between[of=A and B];
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

答案1

您可以soft clip在第一个和第二个图表中使用一个选项,并在第二个图表中使用第三个路径:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[ymin = 0, axis x line = bottom, axis y line=middle, 
             legend pos=outer north east]
\addplot [name path=A, domain=-5:5,cyan, thick]{x^2 + 2*x + 2};
\addplot [name path=B, domain=0:10,orange,thick]{2*x + 2};
\legend{$f(x)$, $g(x)$}
\addplot[blue!20] fill between[of=A and B, 
       soft clip={domain={0:10}}]; %<--------- added
\end{axis}
\end{tikzpicture}
\end{center}

\begin{center}
\begin{tikzpicture}
\begin{axis}[ymin = 0, axis x line = bottom, axis y line=middle, 
             legend pos=outer north east]
\addplot [name path=A, domain=-5:5,cyan, thick]{x^2 + 2*x + 2};
\addplot [name path=B, domain=0:10,orange,thick]{2*x + 2};
\legend{$f(x)$, $g(x)$}

\path [name path=yaxis] (0,0)--(0,35);  %<---- third path

\addplot[blue!20] fill between[of=A and B, 
         soft clip={domain={0:10}}]; %<---- added soft clip
\addplot[blue!20] fill between[of=A and yaxis, 
         soft clip={domain={0:10}}];  %<----- added second region
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容