答案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}