![PGFPlot 的“域”参数被“表”或“坐标”输入忽略](https://linux22.com/image/308833/PGFPlot%20%E7%9A%84%E2%80%9C%E5%9F%9F%E2%80%9D%E5%8F%82%E6%95%B0%E8%A2%AB%E2%80%9C%E8%A1%A8%E2%80%9D%E6%88%96%E2%80%9C%E5%9D%90%E6%A0%87%E2%80%9D%E8%BE%93%E5%85%A5%E5%BF%BD%E7%95%A5.png)
最小示例:
\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{compat=newest} % has no effect here
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
% Does not work with table or coordinates
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[smooth] coordinates {\pts};
\addplot[smooth, fill=red, domain={0.2:0.25}] coordinates {\pts} \closedcycle;
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[]
% Does not work with table or coordinates
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[smooth] coordinates {\pts};
\addplot[smooth, fill=red, restrict x to domain={0.2:0.25}] coordinates {\pts} \closedcycle;
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[]
% Works with expression
\addplot[smooth, domain=0:20] {x*x};
\addplot[smooth, fill=blue, domain={10.5:12}] {x*x} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
结果:
第二张图的问题在于绘图不规则(因为平滑/插值算法没有相同的输入)。
答案1
正如评论中指出的那样,smooth
域限制不能很好地协同工作以填充曲线和轴之间的区域。
但是,您可以使用fillbetween
允许填充任意两个图之间的路径的库。为了使用它,我们需要将 X 轴添加为不可见(但有名称)的虚拟图,并使用soft clip
以限制填充区域的范围:
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[name path=PTS,smooth] coordinates {\pts};
\path[name path=AXIS] (0.1,0) -- (0.3,0);
\addplot[fill=red] fill between[of=PTS and AXIS,soft clip={domain={0.2:0.25}}];
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[]
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[name path=PTS,smooth] coordinates {\pts};
\path[name path=AXIS] (0.1,0) -- (0.3,0);
\addplot[fill=red] fill between[of=PTS and AXIS,soft clip={domain={0.2:0.25}}];
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[]
\addplot[name path=PTS,smooth, domain=0:20] {x*x};
\path[name path=AXIS] (0,0) -- (20,0);
\addplot[fill=blue] fill between[of=PTS and AXIS,soft clip={domain={10.5:12}}];
\end{axis}
\end{tikzpicture}
\end{document}