如何在 Tikz 中创建带有阴影区域的图形

如何在 Tikz 中创建带有阴影区域的图形

我想为 函数创建图形y = 1/x。和x > 0之间的曲线下面积为。和之间的曲线下面积为。x = ax = 1A1x = 1x = bA2

我不知道如何为图中所示的区域添加阴影。 在此处输入图片描述

答案1

其中大部分来自手册第 103 页底部的示例,和的x定位y来自这个答案

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.16,width=10cm}
\begin{document}
\begin{tikzpicture}[declare function={a=0.5;b=3;f(\x)=1/\x;}]
 \begin{axis}[axis lines=middle,axis on top,xlabel=$x$,ylabel=$y$,
 xmin=-0.5,xmax=5,ymin=-0.5,ymax=2,ytick=\empty,
 xtick={a,1,b},xticklabels={$a$,$1$,$b$}, % https://tex.stackexchange.com/a/68407/121799
 every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
 every axis y label/.style={at={(current axis.above origin)},anchor=north east}]
  \addplot[name path=A,blue,thick,domain=0.2:5,smooth] {f(x)};
  \path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
  \addplot [gray!30] fill between [
        of=A and B,soft clip={domain=a:1},
    ];
    \addplot [gray] fill between [
        of=A and B,soft clip={domain=1:b},
    ];
    \path ({(1+a)/2},{f((1+a)/2)/2}) node{$A_1$}
    ({(1+b)/2},{f((1+b)/2)/2}) node{$A_2$};
 \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容