我想填充由坐标轴和双曲线 $xy=1$ 的第一象限部分所包围的区域。但该区域恰好遮住了我的 x 轴(以及靠近原点的 y 轴)。有没有办法解决这个问题?代码如下:
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{figure}
\centering{
\begin{tikzpicture}
\begin{axis}[xmin=-4, xmax=4, ymin=-4, ymax=4,
axis lines = middle,
x label style={at={(axis cs:4,0)}, anchor=west},
y label style={at={(axis cs:0,4)}, anchor=south},
xlabel=$x$, ylabel=$y$, ticks=none,
xticklabels={,,}, yticklabels={,,},
unit vector ratio*=1 1 1, unbounded coords=jump]
\addplot[name path = a, domain=0.1:4] ({x},{1/x});
\path[name path=paxis] (axis cs:0,0) -- (axis cs:4,0);
\addplot[yellow] fill between[of=a and paxis];
\end{axis}
\end{tikzpicture}
}
\end{figure}
谢谢。
答案1
可能有更好的方法,但您可以为无界部分创建其他路径(名为aa
和bb
)。另请注意 的使用axis on top
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}
\centering{
\begin{tikzpicture}
\begin{axis}[xmin=-4, xmax=4, ymin=-4, ymax=4,
axis on top,
axis lines = middle,
x label style={at={(axis cs:4,0)}, anchor=west},
y label style={at={(axis cs:0,4)}, anchor=south},
xlabel=$x$, ylabel=$y$, ticks=none,
xticklabels={,,}, yticklabels={,,},
unit vector ratio*=1 1 1, unbounded coords=jump]
\addplot[name path = a, domain=0.25:4] ({x},{1/x});
\path[name path=paxis] (axis cs:.25,0) -- (axis cs:4,0);
\path[name path=aa] (axis cs:0,0) -- (axis cs:.25,0);
\path[name path=bb] (axis cs:0,4) -- (axis cs:.25,4);
\addplot[yellow] fill between[of=a and paxis];
\addplot[yellow] fill between[of=aa and bb];
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}
答案2
另一种可能性是:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
axis on top,
xmin=-4, xmax=4,
ymin=-4, ymax=4,
x label style={anchor=west},
y label style={anchor=south},
xlabel=$x$, ylabel=$y$,
ticks=none,
domain=0.1:4
]
\addplot[draw=none, fill=yellow] {1/x} |- (0,0) -- (0,4);
\addplot[thick] {1/x};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
注意,在 1.11 版之后,axis preamble 被视为 ˙pgfplots` 的语法。