![使用 tikz 和 pgfplots 绘制阴影区域](https://linux22.com/image/470545/%E4%BD%BF%E7%94%A8%20tikz%20%E5%92%8C%20pgfplots%20%E7%BB%98%E5%88%B6%E9%98%B4%E5%BD%B1%E5%8C%BA%E5%9F%9F.png)
我能做的最好的是以下代码:
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\begin{axis}
[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=2, ymin=0, ymax=3,
xmajorticks=false
]
\addplot[smooth, thick, samples=200, name path=curve1] {1/(2*x)};
\addplot[smooth, thick, samples=200, name path=curve2] {1/x};
\addplot[smooth, thick, samples=200, name path=curve3] {2*x^2};
\addplot[smooth, thick, samples=200, name path=curve4] {3*x^2};
\addplot[color=black,fill=pink,fill opacity=0.4]fill between[of=curve1 and curve2, soft clip={domain=0.5503:0.7937}];
\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}
这将创建下面的图像:
有人能帮帮我吗?因为我不知道如何使用函数 y=1/2x 和 y=1/x 作为阴影区域的边框。
答案1
这是一个非常 hacky 的解决方案,但它至少填充了您要查找的区域。问题是它填充了外面的区域,然后将其覆盖,这不太一样。我会继续思考如何“正确”地做到这一点,如果我想到了什么,我会更新!
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=2, ymin=0, ymax=3,
xmajorticks=false
]
\addplot[smooth, thick, samples=200, name path=curve1] {1/(2*x)};
\addplot[smooth, thick, samples=200, name path=curve2] {1/x};
\addplot[smooth, thick, samples=200, name path=curve3] {2*x^2};
\addplot[smooth, thick, samples=200, name path=curve4] {3*x^2};
\addplot[color=black,fill=pink,fill opacity=0.4]fill between[of=curve1 and curve2, soft clip={domain=0.5503:0.7937}];
\addplot[color=black,fill=white,fill opacity=1.0]fill between[of=curve2 and curve4, soft clip={domain=0.5503:0.8}];
\addplot[color=black,fill=white,fill opacity=1.0]fill between[of=curve1 and curve3, soft clip={domain=0.5503:0.8}];
\end{axis}
\end{tikzpicture}
\end{document}
编辑:这是一个稍微不那么黑客化的版本,因为它不需要任何背景掩盖:
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=2, ymin=0, ymax=3,
xmajorticks=false
]
\addplot[smooth, thick, samples=200, name path=curve1] {1/(2*x)};
\addplot[smooth, thick, samples=200, name path=curve2] {1/x};
\addplot[smooth, thick, samples=200, name path=curve3] {2*x^2};
\addplot[smooth, thick, samples=200, name path=curve4] {3*x^2};
\addplot[fill=pink,fill opacity=0.4] fill between[of=curve1 and curve4,
soft clip={domain=0.5503:0.6299605}];
\addplot[fill=pink,fill opacity=0.4] fill between[of=curve3 and curve4,
soft clip={domain=0.6299605:0.69335}];
\addplot[fill=pink,fill opacity=0.4] fill between[of=curve2 and curve3,
soft clip={domain=0.69335:0.7937}];
\end{axis}
\end{tikzpicture}
\end{document}