错误:使用多个图填充

错误:使用多个图填充

fillbetween两次使用tikzpicture相同的pgfplots 包name path会产生不同的结果。

在此处输入图片描述

梅威瑟:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.10}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{poly}{0}{\pgfmathparse{-x^3+5*(x^2)-3*x-3}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xtick       = {-1.2,2,4.2},
    xticklabels = {$a$,$\zeta$,$b$},
    ytick       = {3},
    yticklabels = {$f(\zeta)$},
    samples     = 160,
    domain      = -1.2:4.2,
    xmin = -2, xmax = 5,
    ymin = -5, ymax = 10,
  ]
  \addplot[name path=poly, black, thick, mark=none, ] {poly};
  \addplot[name path=line, gray, no markers, line width=1pt] {3};
  \addplot fill between[ 
    of = poly and line, 
    split, % calculate segments
    every even segment/.style = {orange!70},
    every odd segment/.style  = {gray!60}
  ];
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
    axis y line = left,
    axis x line = bottom,
    xtick       = {-1.2,2,4.2},
    xticklabels = {$a$,$\zeta$,$b$},
    ytick       = {3},
    yticklabels = {$f(\zeta)$},
    samples     = 160,
    domain      = -1.2:4.2,
    xmin = -2, xmax = 5,
    ymin = -5, ymax = 10,
  ]
  \addplot[name path=poly, black, thick, mark=none, ] {poly};
  \addplot[name path=line, gray, no markers, line width=1pt] {3};
  \addplot fill between[ 
    of = poly and line, 
    split, % calculate segments
    every even segment/.style = {orange!70},
    every odd segment/.style  = {gray!60}
  ];
\end{axis}
\end{tikzpicture}
\end{document}

有解决方法吗?我无法使用不同的方法,name path因为图表是由脚本生成的。

答案1

我在另一张票中找到了解决方案:http://sourceforge.net/p/pgfplots/bugs/79/

Christian Feuersänger 的回答是:

感谢您提供详细的输出!您的列表表明您的系统使用的是 PGF 2.10(PGF 的先前稳定版本)。在这种情况下,我可以重现该问题。

根本原因分析显示,pgfplots 成功检测到它有一个过时的 PGF,但它无法为此版本应用自动补丁。这是 pgfplots 中的一个错误,我会处理它。解决方案是升级到 PGF 3.0.0(或更新版本)。

解决方法:

% PATCH TO FIX INCOMPATIBILITY BETWEEN PGFPLOTS 1.12 AND PGF 2.10
\makeatletter
\def\tikz@intersect@addto@path@names#1#2{%
\edef\tikz@marshal{#2\expandafter\noexpand\csname tikz@intersect@path@name@#1\endcsname}%
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@marshal%
\expandafter\expandafter\expandafter{\expandafter\tikz@marshal\expandafter{\tikz@intersect@temppath}}%
\expandafter\pgfutil@g@addto@macro\expandafter\tikz@intersect@namedpaths\expandafter{\tikz@marshal}%
}
\pgfkeys{%
/tikz/name path global/.code={%
\expandafter\global\expandafter\let\csname tikz@intersect@path@name@#1\endcsname=\relax
\tikz@key@name@path@new{#1}{\gdef}%
},
/tikz/name path local/.code={%
\pgfkeys{/tikz/name path={#1}}%
},
/tikz/name path/.code={%
\tikz@key@name@path@new{#1}{\def}%
},
}
\makeatother

相关内容