4

I'm trying to get fillbetween to fill the area of two graphs in two seperate plots:

http://i.imgur.com/Qnnvjjc.png

I've set the same domain for both fillings, but it seems to be filling different domains. (quite frankly I'm not even sure what it does)

Am I doing something wrong or is this a bug?

I'd like to mention that excluding geometry somehow seems to make the filling more "accurate" (but it's still off miles).

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{geometry} % try commenting this
\usepackage{hyperref}
\usepackage{hypcap}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\usetikzlibrary{shadows}

\pgfmathdeclarefunction{normal}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
%
\makeatletter
\pgfmathdeclarefunction{erf}{1}{%
  \begingroup
    \pgfmathparse{#1 > 0 ? 1 : -1}%
    \edef\sign{\pgfmathresult}%
    \pgfmathparse{abs(#1)}%
    \edef\x{\pgfmathresult}%
    \pgfmathparse{1/(1+0.3275911*\x)}%
    \edef\t{\pgfmathresult}%
    \pgfmathparse{%
      1 - (((((1.061405429*\t -1.453152027)*\t) + 1.421413741)*\t 
      -0.284496736)*\t + 0.254829592)*\t*exp(-(\x*\x))}%
    \edef\y{\pgfmathresult}%
    \pgfmathparse{(\sign)*\y}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup
}
\pgfmathdeclarefunction{qnorm}{4}{
  \begingroup
    \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
    \pgfmathparse{#1}
    \edef\p{\pgfmathresult}
    \pgfmathparse{#2}
    \edef\mu{\pgfmathresult}
    \pgfmathparse{#3}
    \edef\sigma{\pgfmathresult}
    %
    \pgfmathparse{\sigma < 0 ? 1 : 0}           
    \edef\sigma@neg{\pgfmathresult}
    \pgfmathparse{\sigma == 0 ? 1 : 0}          
    \edef\sigma@zero{\pgfmathresult}
    %
    \pgfmathparse{\p - .5}                  
    \edef\q{\pgfmathresult}
    %
    \pgfmathparse{abs(\q) <= .425 ? 1 : 0}      
    \edef\q@i{\pgfmathresult}               
    %
    \pgfmathparse{                      
      \q@i ?                        
        .180625 - \q * \q               
      :                     
        sqrt(-ln(min(\p,(1-\p))))                        
    }
    \edef\r@{\pgfmathresult}
   % 
    \pgfmathparse{\q@i ? 
      \r@ 
    : 
      (\r@ <= 5 ?  
        \r@ - 1.6 %
      : 
        \r@ - 5
      )}
    \edef\r{\pgfmathresult}
    \pgfmathparse{\q@i ? 
    (\q * (((((((\r * 2509.0809287301226727 +
                       33430.575583588128105) * \r + 67265.770927008700853) * \r +
                     45921.953931549871457) * \r + 13731.693765509461125) * \r +
                   1971.5909503065514427) * \r + 133.14166789178437745) * \r +
                 3.387132872796366608)
            / (((((((\r * 5226.495278852854561 +
                     28729.085735721942674) * \r + 39307.89580009271061) * \r +
                   21213.794301586595867) * \r + 5394.1960214247511077) * \r +
                 687.1870074920579083) * \r + 42.313330701600911252) * \r + 1.))
                                    : ( \r@ <= 5 ?  
                                        ((((((((\r * 7.7454501427834140764e-4 +     %
                       .0227238449892691845833) * \r + .24178072517745061177) *     %
                     \r + 1.27045825245236838258) * \r +                        %
                    3.64784832476320460504) * \r + 5.7694972214606914055) *     %
                  \r + 4.6303378461565452959) * \r +                        %
                 1.42343711074968357734)                                %
                / (((((((\r *                                       %
                         1.05075007164441684324e-9 + 5.475938084995344946e-4) *     %
                        \r + .0151986665636164571966) * \r +                    %
                       .14810397642748007459) * \r + .68976733498510000455) *       %
                     \r + 1.6763848301838038494) * \r +                     %
                    2.05319162663775882187) * \r + 1))                      %
                                        :       
                                        ((((((((\r * 2.01033439929228813265e-7 +
                       2.71155556874348757815e-5) * \r +
                      .0012426609473880784386) * \r + .026532189526576123093) *
                    \r + .29656057182850489123) * \r +
                   1.7848265399172913358) * \r + 5.4637849111641143699) *
                 \r + 6.6579046435011037772)
                / (((((((\r *
                         2.04426310338993978564e-15 + 1.4215117583164458887e-7)*
                        \r + 1.8463183175100546818e-5) * \r +
                       7.868691311456132591e-4) * \r + .0148753612908506148525)
                     * \r + .13692988092273580531) * \r +
                    .59983220655588793769) * \r + 1.))
                    )
                 }
    \edef\val{\pgfmathresult}
    \pgfmathparse{\q@i ? \val :
    (\q > 0 ? \val : -\val)}                    %
    \edef\@tmp{\pgfmathresult}
    \pgfmathparse{\p > 1 ? sqrt(-1) :
      (\p < 0 ? sqrt(-1) :
        \sigma@neg ? sqrt(-1) :             %
         (\sigma@zero ? \mu :               %
           (#4 ? \mu+\sigma*\@tmp :         
                     \mu-\sigma*\@tmp)              % 
         )
       )
    }
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
\makeatother
\pgfmathdeclarefunction{skew}{3}{%
        \pgfmathparse{(exp(-((x-#1)^2)/(2*(#2)^2))*((erf((#3*(x-#1))/(sqrt(2)*#2)))+1))/(sqrt(2*pi)*#2)}%
}
\begin{document}

\newcommand{\samplesize}{52} % SET sample size globally

\newcommand*\quantile{.95}\relax
\newcommand*\mean{8}\relax
\newcommand*\sd{3.416969}\relax
\newcommand*\lowertail{1}\relax


\pgfmathsetmacro\qqq{qnorm(\quantile,\mean,\sd,\lowertail)}

\begin{figure}[ht]
\capstart
\begin{center}
\begin{tabular}{c}
\begin{tikzpicture}
\begin{axis}[
      hide y axis,
      axis lines*=center, 
      axis on top,
      no markers, 
      domain=0:21, 
      samples=4*\samplesize-7,
      xlabel=\empty, 
      ylabel=\empty,
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      width=.8\linewidth,
      height=.3\linewidth,
      xmin = 0, xmax=21,
      xtick=, ytick=\empty,
      enlargelimits=false, 
      clip=false
  ]
  \addplot [name path=normaal,very thick,cyan!85!black!50] {normal(\mean,\sd)};
  \path[name path=axis] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
  \addplot[red!30,draw=red!80!black]   fill between[of=normaal and axis,soft clip={domain=\qqq:\pgfkeysvalueof{/pgfplots/xmax}}];
\end{axis}
\end{tikzpicture} \\
\begin{tikzpicture}
\begin{axis}[
      hide y axis,
      axis lines*=center, 
      axis on top,
      no markers, 
      domain=0:21, 
      samples=4*\samplesize-7,
      xlabel=\empty, 
      ylabel=\empty,
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      width=.8\linewidth,
      height=.3\linewidth,
      xmin = 0, xmax=21,
      xtick=, ytick=\empty,
      enlargelimits=false, 
      clip=false
  ]
  \addplot [name path=skew,very thick,green!85!black!50] {skew(19,4,-6)};
  \path[name path=axis] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
  \addplot[red!30,draw=red!80!black]   fill between[of=skew and axis,soft clip={domain=\qqq:\pgfkeysvalueof{/pgfplots/xmax}}];
\end{axis}
\end{tikzpicture}
\end{tabular}
\caption{De statistische power is de kans dat je een effect vindt als het er ook echt is}\label{fig:power}
\end{center}
\end{figure}

\end{document}
  • For the record: With the release of PGFPlots v1.16 your example gives the expected result. – Stefan Pinnow Apr 7 '18 at 4:41
6

This looks a lot like a bug.

You can work around it by not using the domain option to specify the soft clip, but instead using the rectangle option like so:

soft clip={(axis cs:\qqq,0) rectangle (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax}*1.1)}

Note that the upper right-hand corner of the rectangle has to be higher than the largest y value, in this case I increased it by 10 %, otherwise the bug also appears.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{geometry} % try commenting this
\usepackage{hyperref}
\usepackage{hypcap}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\usetikzlibrary{shadows}

\pgfmathdeclarefunction{normal}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
%
\makeatletter
\pgfmathdeclarefunction{erf}{1}{%
  \begingroup
    \pgfmathparse{#1 > 0 ? 1 : -1}%
    \edef\sign{\pgfmathresult}%
    \pgfmathparse{abs(#1)}%
    \edef\x{\pgfmathresult}%
    \pgfmathparse{1/(1+0.3275911*\x)}%
    \edef\t{\pgfmathresult}%
    \pgfmathparse{%
      1 - (((((1.061405429*\t -1.453152027)*\t) + 1.421413741)*\t 
      -0.284496736)*\t + 0.254829592)*\t*exp(-(\x*\x))}%
    \edef\y{\pgfmathresult}%
    \pgfmathparse{(\sign)*\y}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup
}
\pgfmathdeclarefunction{qnorm}{4}{
  \begingroup
    \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
    \pgfmathparse{#1}
    \edef\p{\pgfmathresult}
    \pgfmathparse{#2}
    \edef\mu{\pgfmathresult}
    \pgfmathparse{#3}
    \edef\sigma{\pgfmathresult}
    %
    \pgfmathparse{\sigma < 0 ? 1 : 0}           
    \edef\sigma@neg{\pgfmathresult}
    \pgfmathparse{\sigma == 0 ? 1 : 0}          
    \edef\sigma@zero{\pgfmathresult}
    %
    \pgfmathparse{\p - .5}                  
    \edef\q{\pgfmathresult}
    %
    \pgfmathparse{abs(\q) <= .425 ? 1 : 0}      
    \edef\q@i{\pgfmathresult}               
    %
    \pgfmathparse{                      
      \q@i ?                        
        .180625 - \q * \q               
      :                     
        sqrt(-ln(min(\p,(1-\p))))                        
    }
    \edef\r@{\pgfmathresult}
   % 
    \pgfmathparse{\q@i ? 
      \r@ 
    : 
      (\r@ <= 5 ?  
        \r@ - 1.6 %
      : 
        \r@ - 5
      )}
    \edef\r{\pgfmathresult}
    \pgfmathparse{\q@i ? 
    (\q * (((((((\r * 2509.0809287301226727 +
                       33430.575583588128105) * \r + 67265.770927008700853) * \r +
                     45921.953931549871457) * \r + 13731.693765509461125) * \r +
                   1971.5909503065514427) * \r + 133.14166789178437745) * \r +
                 3.387132872796366608)
            / (((((((\r * 5226.495278852854561 +
                     28729.085735721942674) * \r + 39307.89580009271061) * \r +
                   21213.794301586595867) * \r + 5394.1960214247511077) * \r +
                 687.1870074920579083) * \r + 42.313330701600911252) * \r + 1.))
                                    : ( \r@ <= 5 ?  
                                        ((((((((\r * 7.7454501427834140764e-4 +     %
                       .0227238449892691845833) * \r + .24178072517745061177) *     %
                     \r + 1.27045825245236838258) * \r +                        %
                    3.64784832476320460504) * \r + 5.7694972214606914055) *     %
                  \r + 4.6303378461565452959) * \r +                        %
                 1.42343711074968357734)                                %
                / (((((((\r *                                       %
                         1.05075007164441684324e-9 + 5.475938084995344946e-4) *     %
                        \r + .0151986665636164571966) * \r +                    %
                       .14810397642748007459) * \r + .68976733498510000455) *       %
                     \r + 1.6763848301838038494) * \r +                     %
                    2.05319162663775882187) * \r + 1))                      %
                                        :       
                                        ((((((((\r * 2.01033439929228813265e-7 +
                       2.71155556874348757815e-5) * \r +
                      .0012426609473880784386) * \r + .026532189526576123093) *
                    \r + .29656057182850489123) * \r +
                   1.7848265399172913358) * \r + 5.4637849111641143699) *
                 \r + 6.6579046435011037772)
                / (((((((\r *
                         2.04426310338993978564e-15 + 1.4215117583164458887e-7)*
                        \r + 1.8463183175100546818e-5) * \r +
                       7.868691311456132591e-4) * \r + .0148753612908506148525)
                     * \r + .13692988092273580531) * \r +
                    .59983220655588793769) * \r + 1.))
                    )
                 }
    \edef\val{\pgfmathresult}
    \pgfmathparse{\q@i ? \val :
    (\q > 0 ? \val : -\val)}                    %
    \edef\@tmp{\pgfmathresult}
    \pgfmathparse{\p > 1 ? sqrt(-1) :
      (\p < 0 ? sqrt(-1) :
        \sigma@neg ? sqrt(-1) :             %
         (\sigma@zero ? \mu :               %
           (#4 ? \mu+\sigma*\@tmp :         
                     \mu-\sigma*\@tmp)              % 
         )
       )
    }
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}
\makeatother
\pgfmathdeclarefunction{skew}{3}{%
        \pgfmathparse{(exp(-((x-#1)^2)/(2*(#2)^2))*((erf((#3*(x-#1))/(sqrt(2)*#2)))+1))/(sqrt(2*pi)*#2)}%
}
\begin{document}

\newcommand{\samplesize}{52} % SET sample size globally

\newcommand*\quantile{.95}\relax
\newcommand*\mean{8}\relax
\newcommand*\sd{3.416969}\relax
\newcommand*\lowertail{1}\relax


\pgfmathsetmacro\qqq{qnorm(\quantile,\mean,\sd,\lowertail)}

\begin{figure}[ht]
\capstart
\begin{center}
\begin{tabular}{c}
\begin{tikzpicture}
\begin{axis}[
      hide y axis,
      axis lines*=center, 
      axis on top,
      no markers, 
      domain=0:21, 
      samples=4*\samplesize-7,
      xlabel=\empty, 
      ylabel=\empty,
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      width=.8\linewidth,
      height=.3\linewidth,
      xmin = 0, xmax=21,
      xtick=, ytick=\empty,
      enlargelimits=false, 
      clip=false
  ]
  \addplot [name path=normaal,very thick,cyan!85!black!50] {normal(\mean,\sd)};
  \path[name path=axis] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
  \addplot[red!30,draw=red!80!black]   fill between[of=normaal and axis,soft clip={domain=\qqq:\pgfkeysvalueof{/pgfplots/xmax}}];
\end{axis}
\end{tikzpicture} \\
\begin{tikzpicture}
\begin{axis}[
      hide y axis,
      axis lines*=center, 
      axis on top,
      no markers, 
      domain=0:21, 
      samples=4*\samplesize-7,
      xlabel=\empty, 
      ylabel=\empty,
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      width=.8\linewidth,
      height=.3\linewidth,
      xmin = 0, xmax=21,
      xtick=, ytick=\empty,
      enlargelimits=false, 
      clip=false
  ]
  \addplot [name path=skew,very thick,green!85!black!50] {skew(19,4,-6)};
  \path[name path=axis] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
  \addplot[red!30,draw=red!80!black]   fill between[of=skew and axis,soft clip={(axis cs:\qqq,0) rectangle (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax}*1.1)}];
\end{axis}
\end{tikzpicture}
\end{tabular}
\caption{De statistische power is de kans dat je een effect vindt als het er ook echt is}\label{fig:power}
\end{center}
\end{figure}

\end{document}
| improve this answer | |

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