他,我正在尝试使用 tikz 和 pgfplots 在 Latex 上绘制以下图像
使用以下代码
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=2.5, ymin=0, ymax=2.5,
xmajorticks=false,
ymajorticks=false,
legend style={at={(0,1)},anchor=north east}
]
\addplot[smooth, thick, samples=200, red, name path=curve1] ({x},{x^2/2});
\addlegendentry{$\frac{x^{2}}{2}$}
\addplot[smooth, thick, samples=200, name path=curve2] ({x},{x^2});
\addlegendentry{$x^{2}$}
\addplot[smooth, thick, samples=200, blue, name path=curve3] ({x^2},{x});
\addlegendentry{$y^2$}
\addplot[smooth, thick, samples=200, teal, name path=curve4] ({x^2/2},{x});
\addlegendentry{$\frac{y^{2}}{2}$}
\addplot[fill=magenta,fill opacity=0.4] fill between[of=curve2 and curve3,
soft clip={domain=1:1.2599}];
\addplot[fill=magenta,fill opacity=0.4] fill between[of=curve3 and curve4,
soft clip={domain=1.2599:1.5874}];
\addplot[fill=magenta,fill opacity=0.4] fill between[of=curve1 and curve4,
soft clip={domain=1.5874:2}];
\addlegendentry{$D$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}
我得到了这个结果
这显然不是我想要的。我认为问题在于我试图遮蔽 x 相关函数和 y 相关函数之间的区域,但我不知道如何解决这个问题。有人能帮我吗?
答案1
我会逐段构建区域。虽然这split
允许我们
\addplot[path only] fill between[
of=blackblue and redteal,
split,
every segment no 1/.style={fill=magenta!40}];
仅填充中间区域,图例条目就没有颜色。
对我来说最简单的方法是使用\addplot[fill=magenta!40]
并no segments={0, 2}
禁用其他区域的填充:
\addplot[fill=magenta!40] fill between[
of=blackblue and redteal,
split,
no segments={0, 2}];
代码
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\tikzset{
fill between/no segments/.style={% better for legendentry
/utils/temp/.style={every segment no ##1/.style=path only},
/utils/temp/.list={#1}}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=2.5, ymin=0, ymax=2.5,
xmajorticks=false,
ymajorticks=false,
legend style={at={(0,1)},anchor=north east},
thick, samples=200, smooth,
]
\addplot[red, name path=red ] ({x},{x^2/2});
\addlegendentry{$\frac{x^{2}}{2}$}
\addplot[ name path=black] ({x},{x^2});
\addlegendentry{$x^{2}$}
\addplot[blue, name path=blue ] ({x^2},{x});
\addlegendentry{$y^2$}
\addplot[teal, name path=teal ] ({x^2/2},{x});
\addlegendentry{$\frac{y^{2}}{2}$}
\path[name path=blackblue, intersection segments={of=black and blue, sequence={L3[reverse] -- R3}}];
\path[name path=redteal, intersection segments={of=red and teal, sequence={L2 -- R2[reverse]}}];
\addplot[fill=magenta!40] fill between[of=blackblue and redteal, split, no segments={0, 2}];
\addlegendentry{$D$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel={$x$},
ylabel={$y$},
xmin=-1, xmax=3.5, ymin=0, ymax=10.5,
domain=0:3.5,
xmajorticks=false,
ymajorticks=false,
legend style={at={(0,1)},anchor=north east},
thick, samples=125, smooth,
]
\addplot[red, name path=red ] ({x},{2*x});
\addlegendentry{$y=2x$}
\addplot[ name path=black] ({x},{x});
\addlegendentry{$y=x$}
\addplot[blue, name path=blue, domain=.1:3.5] ({x},{2*pi/x});
\addlegendentry{$y=2\pi/x$}
\addplot[teal, name path=teal, domain=.1:3.5] ({x},{3*pi/x});
\addlegendentry{$y=3\pi/x$}
\path[name path=blackblue, intersection segments={of=black and blue, sequence={R1 -- L2}}];
\path[name path=redteal, intersection segments={of=red and teal, sequence={L1 -- R2}}];
\addplot[fill=magenta!40] fill between[of=blackblue and redteal, split, no segments={0, 2}];
\addlegendentry{$D$}
\end{axis}
\end{tikzpicture}
\end{document}