为曲线下方和上方的表面着色

为曲线下方和上方的表面着色

定义曲线如下:

\begin{tikzpicture}
    \draw[->] (0,0) -- (8,0) node[right] {$t$};

    \draw[name path=black_function, thick] (0,2) .. controls (2,3) and (3,2) .. (4,1)
     .. controls (5,0) and (6,2) .. (8,0.2);
    % Add vertical lines and labels
    \draw (1, 0.2) -- (1, -0.2) node[below] {$0$};
    \draw (7, 0.2) -- (7, -0.2) node[below] {$T$};
    \draw (3, 0.2) -- (3, -0.2) node[below] {$\tau$};
\end{tikzpicture}

我想在它的下面涂上浅红色,在它的上面涂上浅蓝色。

答案1

使用pgfplots.fillbetweenTiZ 库:

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}    % loads tikz too
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows.meta,
                backgrounds,
                pgfplots.fillbetween,
                }

\begin{document}
    \begin{tikzpicture}
\path[name path=A, fill=blue]  (0,4) -- (8,4);
\draw[name path=B, -Straight Barb, semithick]  (0,0) -- (8,0);
\foreach \i [count=\x from 0] in {0, \tau, T}
    \draw   (1+3*\x,0.1) -- ++ (0,-0.2) node[below] {$\i$};
\draw[name path=C, very thick]
    (0,2)   .. controls (2,3) and (3,2) .. (4,1)
            .. controls (5,0) and (6,2) .. (8,0.2);
% fillbetween
\scoped[on background layer]
{
\tikzfillbetween[of=A and C]    {blue};
\tikzfillbetween[of=B and C]    {red};
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一个简单的解决方案是

\documentclass{article}
\usepackage{tikz}


\begin{document}
    
\begin{tikzpicture}
    \draw[->] (0,0) -- (8,0) node[right] {$t$};
    
        \fill[red, fill opacity=0.5] (0,2) .. controls (2,3) and (3,2) .. (4,1)
    .. controls (5,0) and (6,2) .. (8,0.2) -- (8,0)--(0,0)--(0,2)--cycle;
\def\H{5}
    \fill[blue, fill opacity=0.5,] (0,2) .. controls (2,3) and (3,2) .. (4,1)
.. controls (5,0) and (6,2) .. (8,0.2) --(8,\H) --(0,\H) --(0,2)--cycle;
    
    \draw[, thick] (0,2) .. controls (2,3) and (3,2) .. (4,1)
    .. controls (5,0) and (6,2) .. (8,0.2);
    

    % Add vertical lines and labels
    \draw (1, 0.2) -- (1, -0.2) node[below] {$0$};
    \draw (7, 0.2) -- (7, -0.2) node[below] {$T$};
    \draw (3, 0.2) -- (3, -0.2) node[below] {$\tau$};
\end{tikzpicture}
    
\end{document}

您可以更改该\H参数。

相关内容