PGFPlot 的“域”参数被“表”或“坐标”输入忽略

PGFPlot 的“域”参数被“表”或“坐标”输入忽略

最小示例:

\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}

在此处输入图片描述

相关内容