如何使用 TikZ 用定义的颜色填充区域?

如何使用 TikZ 用定义的颜色填充区域?
\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[holdot/.style={circle,draw,fill=white,inner sep=1pt}]
 \draw[thick,-latex] (-2,0) -- (6,0);
 \draw[thick,-latex] (0,-1) -- (0,6);
 \draw[very thick,black!50!black] (-0.8,0.2) -- (3.5,4.5) to[out=45,in=135] 
 node[pos=0.5,above,font=\large]{$f(x)$} (5,4.5);
  \draw[] (1,0) node[below] (x0) {$a$};
  \draw[](4,0) node[below] (x0) {$b$};
  \draw[very thick,dashed,gray!20!gray]  (4,0) -- (4,4.8)  
  \draw[very thick,dashed,gray!20!gray]  (1,0) -- (1,2)  
  \filldraw[fill=gray!20,draw=black] (1,0) --(
\end{tikzpicture}
\end{document}

它产生以下结果

在此处输入图片描述

  • 我如何用颜色填充 a 和 b 之间的区域?
  • 还包括以下文字:$\int_a^b f(x)dx$

提前致谢!

答案1

这是我的尝试:

\documentclass{article}
 
\usepackage{tikz}
\usepackage{amsmath}
    
\begin{document}
\begin{tikzpicture}

% Filled area
  \fill[yellow](1,0) -- (1,2) -- (3.5,4.5) to[out=45,in=35] (4,4.8) -- (4,0) -- cycle;
  
  \node at (2.5,1.25) {\large $\int\limits_{a}^{b} f(x)dx$};
  
 % Axes 
 \draw[thick,-latex] (-2,0) -- (6,0);
 \draw[thick,-latex] (0,-1) -- (0,6);
 
 \draw[very thick,black] (-0.8,0.2) -- (3.5,4.5) to[out=45,in=135] node[pos=0.5,above,font=\large]{$f(x)$} (5,4.5);
 
 \draw[very thick,dashed,gray]  
 (1,0) node[below,black] {$a$} -- (1,2)
 (4,0) node[below,black] {$b$} -- (4,4.8);
  
\end{tikzpicture}

\end{document}

得出的结果是: 在此处输入图片描述

答案2

pgfplots图书馆:pgfplotsfillbetween

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    xmin=-0.8, xmax=5.6, 
    ymin=-0.8, ymax=5.6,
    axis lines=center,
    axis line style={-latex},
    axis on top,
    ticks=none
            ]
% function
\addplot[very thick, smooth, name path=A] coordinates 
{
    (-0.8,0.2)  (3.5,4.5) (5,4.5)
} node[pos=0.9, above=2mm, font=\large]{$f(x)$};
\addplot[draw=none, name path=B] coordinates {(1,0) (4,0)};
% fill
\addplot[yellow] fill between [of = A and B, soft clip={domain=1:4}];
\node[font=\large] at (2.5,1.25) {$\int\limits_{a}^{b} f(x)dx$};
%
\draw[thick, dashed, gray]  
    (1,2.0) -- (1,0) node[below] {$a$}
    (4,4.7) -- (4,0) node[below] {$b$};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容