我正在尝试使用 pgfplots 可视化曲线下的定向面积,如下例所示。但是输出很乱。这里出了什么问题,如何修复?
\documentclass[tikz,convert]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=1.13,
axis lines=center,
samples=100
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=0.49\textwidth]
\addplot[name path = f] gnuplot {cos(x)};%
\addplot[name path = x] gnuplot {0};
%\addplot fill between[of=f and x,split,soft clip={domain=-2:4}];
\addplot fill between[of=f and x,soft clip={domain=-2:4}];
\end{axis}
\end{tikzpicture}
\end{document}
输出:
分割选项使情况变得更好一些,但也不能产生正确的图片。
答案1
这不是真正的解决方案,而是一种旁路:像往常一样绘制函数,并再次将域限制在剪辑间隔内:
\documentclass{article}%[tikz,convert]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=1.13,
axis lines=center,
every fill between plot/.append style={fill=blue,fill opacity=0.2},
samples=100
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=0.49\textwidth]
\addplot[name path = fcurve] gnuplot {cos(x)};%
\addplot[name path = f,domain=-2:4] {cos(x)};%
\addplot[name path = g,domain=-2:4] {0};
\addplot [blue] fill between[of=f and g];
\end{axis}
\end{tikzpicture}
\end{document}
答案2
范围 y 似乎有问题。您可能需要设置 ymin 和 ymax。例如,\begin{axis}[width=0.49\textwidth]
用替换\begin{axis}[ymin=-1.5, ymax=1.5, width=0.49\textwidth]
应该可以。
\documentclass[tikz,convert]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=1.13,
axis lines=center,
samples=100
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=-1.5,
ymax=1.5,
width=0.49\textwidth]
\addplot[name path = f] {cos(deg(x))};%
\addplot[name path = x] {0};
\addplot fill between[of=f and x,soft clip={domain=-2:4}];
\end{axis}
\end{tikzpicture}
\end{document}