3

Using the pgfplots package fillbetween in two tikzpicture with the same name path gives different results.

enter image description here

MWE:

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

Is there a workaround? I cannot use different name path since the plots are generated by a script.

  • 1
    Your MWE works fine here. pgfplots 1.12.1, recently updated TeX Live 2015. – Torbjørn T. Aug 5 '15 at 7:41
  • I am using Windows, pdflatex and have pgfplots 1.12.1 installed. No effect if I set \pgfplotsset{compat=1.12}. – Manuel Schmidt Aug 5 '15 at 11:45
  • 1
    Update TikZ to v.3.0 too – percusse Aug 5 '15 at 14:02
5

I found the solution in another ticket: http://sourceforge.net/p/pgfplots/bugs/79/

The answer from Christian Feuersänger:

Thanks for the detailed output! Your listing indicates that your system has PGF 2.10 (the previous stable of PGF). In this constellation, I can reproduce the problem.

A root cause analysis reveals that pgfplots successfully detects that it has an outdated PGF, but it fails to apply automatic patches for this release. This is a bug in pgfplots and I will take care of it. A solution is to upgrade to PGF 3.0.0 (or newer).

And the workaround:

% 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.