我想对函数 f(x) = 0.5/x 上方的整个区域进行着色,覆盖函数的整个域(在我的情况下为 0.5:47),直到 y=1。换句话说,我希望填充/阴影化我的函数的上轮廓集。
这是我使用的代码:
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = \(a\),
xlabel style={xshift=7.7cm, yshift=0.8cm,},
ylabel = {$\alpha$},
ylabel style={xshift=4cm, yshift=-1.2cm, rotate=-90,},
ymode=log,
yminorgrids=true,
xmax=47,
xmin=0.1,
width=16cm,
height=9cm,
ytick={0.01, 0.1, 1.0},
yticklabels={{$0.01$}, {$0.1$},{$1.0$}},
]
\addplot[name path = A, domain=0:47, samples=100, red] {0.5/x}
node [pos=0.95, above] {$\small{\textcolor{red}{\alpha= \frac{0.5}{a}}}$};
\path[name path=axiss] (0,1) -- (50,1);
\addplot fill between[
of = A and axiss,
soft clip={domain=0.5:47},
every even segment/.style = {gray,opacity=.4}
];
\end{axis}
\end{tikzpicture}
\end{document}
以下是我得到的结果:
注意:我也尝试过:\addplot[name path = A, domain=0:47, samples=100, red, fill=grey] {0.5/x}
但是结果看起来更像是题词而不是我的函数的上轮廓集(因为阴影区域在整个域 0.5:47 上没有上升到 y=1)。
答案1
你几乎已经成功了。
- 删除/评论
soft clip
从fill between
选项 - 添加
split
至fill between
选项
% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
xlabel=\(a\),
xlabel style={xshift=7.7cm, yshift=0.8cm,},
ylabel={$\alpha$},
ylabel style={xshift=4cm, yshift=-1.2cm, rotate=-90,},
ymode=log,
yminorgrids=true,
xmax=47,
xmin=0.1,
width=16cm,
height=9cm,
ytick={0.01, 0.1, 1.0},
yticklabels={{$0.01$}, {$0.1$},{$1.0$}},
]
\addplot [name path=A, domain=0:47, samples=100, red] {0.5/x}
node [pos=0.95, above] {$\small{\textcolor{red}{\alpha= \frac{0.5}{a}}}$};
\path [name path=axiss] (0,1) -- (50,1);
\addplot fill between [
of=A and axiss,
split,
% soft clip={domain=0.5:47},
every even segment/.style={gray,opacity=.4},
];
\end{axis}
\end{tikzpicture}
\end{document}