我的 tikzpicture 代码:
\begin{tikzpicture} [domain=0:4]
\begin{axis}[ axis lines = left, xlabel = $\lambda$, ylabel = {$x$}, ]
\addplot [ fill=red, opacity=.4, domain=1:6, samples=100, color=red, thick, mark=none ] {sqrt((0.1*x^(8)+x^(6)+0.3*x^(2)+3)/(x^(8)))} -- (axis cs:0,40) \closedcycle;
\addlegendentry{$D(\lambda)$}
\addplot [ fill=blue, opacity=.4, domain=1:6, samples=100, color=blue, thick, mark=none ] {sqrt((0.1*x^(8)+x^(6)/3+0.1*x^(2)+5/3)/(x^(8)))} -- (axis cs:0,40) \closedcycle;
\addlegendentry{$f(\lambda)$}
\addplot [ domain=1:6, fill=yellow!25, opacity=.4, samples=100, color=green, thick, mark=none ] {sqrt((0.1*x^(8)-x^(6)-0.3*x^(2)-1)/(x^(8)))} \closedcycle;
\addlegendentry{$g(\lambda)$}
\end{axis}
\end{tikzpicture}
在右侧出现了一条奇怪的对角线:
为什么会发生这种情况?我怎样才能获得图例下的直线?
答案1
效果来自-- (axis cs:0,40)
,它非常高,但不垂直于图的终点。这是获得垂直线的一种方法。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture} [domain=0:4]
\begin{axis}[axis lines = left, xlabel = $\lambda$, ylabel = {$x$}, ]
\addplot[fill=red, opacity=.4,domain=1:6, samples=100,color=red,
thick, mark=none ] {sqrt((0.1*x^(8)+x^(6)+0.3*x^(2)+3)/(x^(8)))}
coordinate[pos=0] (aux0) |- (aux0)
\closedcycle;
\addlegendentry{$D(\lambda)$}
\addplot[fill=blue, opacity=.4,domain=1:6,samples=100,color=blue,
thick, mark=none] {sqrt((0.1*x^(8)+x^(6)/3+0.1*x^(2)+5/3)/(x^(8)))}
|- (aux0)\closedcycle;
\addlegendentry{$f(\lambda)$}
\addplot[domain=3.2:6, fill=yellow!25,
opacity=.4, samples=100, color=green, thick, mark=none ]
{sqrt((0.1*x^(8)-x^(6)-0.3*x^(2)-1)/(x^(8)))} \closedcycle;
\addlegendentry{$g(\lambda)$}
\end{axis}
\end{tikzpicture}
\end{document}