Fillbetween 库根据情节内容有不同的效果

Fillbetween 库根据情节内容有不同的效果

我正在尝试使用 fillbetween 来填充两个独立图中两个图形的面积:

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

我为两种填充设置了相同的域,但它似乎填充了不同的域。(坦率地说,我甚至不确定它的作用)

是我做错了什么还是这是一个错误?

我想说的是,排除geometry某种方式似乎使得填充更加“准确”(但仍然偏离了数英里)。

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

答案1

这看起来很像一个错误。

domain您可以通过不使用该选项来指定来解决该问题soft clip,而是使用rectangle如下选项:

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

注意,矩形的右上角必须高于最大的 y 值,在这种情况下我将其增加了 10%,否则也会出现错误。

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

相关内容