我想沿着\addplot
的线进行剪辑pgfplots
。由于尝试失败,我不太清楚如何操作。到目前为止所做的如下。
\documentclass[tikz, convert = false]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = {x},
ylabel = {y}
]
\addplot[blue, samples = 500, smooth, name path global = pinvx]
gnuplot[domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx]
gnuplot[domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}
\path[name path = line1] (axis cs:1.6, 1) -- (axis cs:1.6, -1);
\path[name path = line2] (axis cs:1.75, 1) -- (axis cs:1.75, -1);
\path[name intersections = {of = pinvx and line1, by = P1}];
\path[name intersections = {of = ninvx and line1, by = P2}];
\path[name intersections = {of = pinvx and line2, by = P3}];
\path[name intersections = {of = ninvx and line2, by = P4}];
\draw[green, opacity =.7] (P1) -- (P2);
\draw[green, opacity =.7] (P3) -- (P4);
\begin{scope}
\clip pinvx;
\fill[green, opacity =.7] (P1) -- (P3) -- (1.75, 0) -- (1.6, 0);
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}
我的最终目标是填充线条和绘图之间的区域1/x
。-1/x
我尝试使用带有第一个绘图名称的剪辑,但不起作用。
这是没有尝试遮蔽该区域的图。
答案1
您可以使用fillbetween
的库pgfplots
,它的使用非常简单:
- 命名路径,
A
例如B
\addplot fill between [of=A and B,...];
,其中...
代表其他选项。
有关更多信息,请参阅第 364 页上的第 5.6 节“填充之间”pgfplots
手动的。
执行
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = $x$,
ylabel = $y$
]
\addplot[blue, samples = 500, smooth, name path global = pinvx]
gnuplot[domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx]
gnuplot[domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}
\addplot fill between [of=pinvx and ninvx,soft clip={domain=1.6:1.75}];
\end{axis}
\end{tikzpicture}
\end{document}
输出
答案2
这是 的结果clip
。首先,您需要定义一个裁剪区域,其坐标与绿线相关联。然后您绘制并填充两个函数,\closedcycle
以便填充曲线下方的区域。但是,实际生成的裁剪区域是上述区域的交集——矩形、1/x 和 -1/x 以下的区域。
代码
\documentclass[tikz, convert = false]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.8}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = {x},
ylabel = {y}
]
\addplot[blue, samples = 500, smooth, name path global = pinvx,domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx, domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}
\path[name path = line1] (axis cs:1.6, 1) -- (axis cs:1.6, -1);
\path[name path = line2] (axis cs:1.75, 1) -- (axis cs:1.75, -1);
\path[name intersections = {of = pinvx and line1, by = P1}];
\path[name intersections = {of = ninvx and line1, by = P2}];
\path[name intersections = {of = pinvx and line2, by = P3}];
\path[name intersections = {of = ninvx and line2, by = P4}];
\draw[green, opacity =.7] (P1) -- (P2);
\draw[green, opacity =.7] (P3) -- (P4);
\begin{scope}
\clip (axis cs: 1.6,-1) rectangle (axis cs:1.75,1);
\addplot[samples = 500, smooth, name path global = pinvx,domain = .5:5,fill=green, opacity =.7] {1/x}\closedcycle;
\addplot[samples = 500, smooth, name path global = ninvx, domain = .5:5,fill=green, opacity =.7] {-1/x}\closedcycle;
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}