pgfplots 填充了错误的段分割

pgfplots 填充了错误的段分割

我希望最后一段是白色的,但是最后一段似乎与前一段结合在一起了。

在此处输入图片描述

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{vdw}{0}{\pgfmathparse{8.314*115/(x-0.0000364)-0.1358/x/x}}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xlabel      = $V$,
    ylabel      = $P$,
    samples     = 500,
    domain      = 0.00005:0.0005,
    xmin = 0, xmax = 0.00055,
    ymin = 0, ymax = 5*10^6,
  ] 
  \addplot[name path=vdw, black, thick, mark=none, ] {vdw}; 
  \addplot[name path=line, gray, no markers, line width=1pt] {2.0*10^6}; 
  \addplot fill between[ 
    of = vdw and line, 
    split, % calculate segments
    every segment no 0/.style={white},
    every segment no 1/.style={orange},
    every segment no 2/.style={red},
    every segment no 3/.style={white},
  ];
\end{axis}

\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

引用 PGFPLOTS 手册(第 5.6.8 节陷阱和局限性,第 390 页),

第一个限制是可扩展性。如果样本数量很大,底层算法效率相对较低,扩展性很差。请将其应用于合理的样本大小”和具有合理交叉点数量的图”。这意味着:如果花费的时间太长,您可能需要降低采样密度。

因此,如果您稍微减少样本数量(想象一下您的步骤中有 500 个样本,9x10^{-7}太密集了),它将给出正确的结果。在这里,我选择samples = 200仅通过反复试验的方法。

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{vdw}{0}{\pgfmathparse{8.314*115/(x-0.0000364)-0.1358/x/x}}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xlabel      = $V$,
    ylabel      = $P$,
    samples     = 200,
    domain      = 0.00005:0.0005,
    xmin = 0, xmax = 0.00055,
    ymin = 0, ymax = 5*10^6,
  ] 
  \addplot[name path=vdw, black, thick, mark=none, ] {vdw}; 
  \addplot[name path=line, gray, no markers, line width=1pt] {2.0*10^6}; 
  \addplot fill between[ 
    of = vdw and line, 
    split, % calculate segments
    every segment no 0/.style={white},
    every segment no 1/.style={orange},
    every segment no 2/.style={red},
    every segment no 3/.style={white},
  ];
\end{axis}

\end{tikzpicture}
\end{document} 

相关内容