1

Basically I am going to use a lot of very similar plots, with a difference in the placement of 2 boundaries, and the position of 2 labels, so I would like to be able to just enter those 2 numbers as arguments, and let LaTeX take care of it. My failed attempt:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{xparse}

\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

%zc,zt
\NewDocumentCommand{\hypplot}{ m m }
    {%
        \begin{tikzpicture}
            \begin{axis}[
                no markers, domain=-4:4, 
                samples=100, axis lines*=left,
                every axis x label/.style={at=(current axis.right of     origin),anchor=west},
                height=5cm, width=12cm, 
                ytick=\empty, xtick=\empty,
                enlargelimits=false, clip=false, axis on top,
                y axis line style={draw=none},
                ]

                %%Shade the area for alpha
                \addplot [fill=cyan!20, draw=none, domain=-4:#1] {gauss(0,1)} \closedcycle; 

                %%Shade the area for p-value
                \addplot [fill=orange!20, draw=none, domain=#1:#2] {gauss(0,1)} \closedcycle; 

                %%draw the gaussian
                \addplot [very thick,cyan!50!black] {gauss(0,1)};

                %%draws a double-headed arrow for Zc
                \draw [latex-latex] (axis cs:#1,-0.07) -- node [fill=white] {$\displaystyle \zcl{c}\frac{\sigma}{\sqrt{n}}$} (axis cs:0,0.-0.07);

                %%draws a double-headed arrow for Zt
                \draw [yshift=-0.6cm, latex-latex] (axis cs:#2,0.16) -- (axis cs:0,0.16);

                %%Label Zt
                \draw [yshift=-0.6cm](axis cs:(#1+#2)/2,0.22) node {$\displaystyle \zcl{T}\frac{\sigma}{\sqrt{n}}$};

                %%label the mean
                \draw (axis cs:0,.43) node {$\mu=\mu_0^{}$};
                \addplot +[mark=none] coordinates {(0, 0) (0, 0.3989)};

                %%add alpha
                \draw (axis cs:#1*1.071,0.02) node {$\alpha$};  
                \addplot +[mark=none] coordinates {(#1, -.01) (#1, 0.08)};
                \draw (axis cs:#1*1.02,0.10) node {$\bar{x}_c^{}$};

                %add p-value border
                \addplot +[mark=none] coordinates {(#2, -.01) (#2, 0.22)};
                \draw (axis cs:#2*1.02,0.25) node {$\bar{x}$};
            \end{axis}
        \end{tikzpicture}%
    }

\begin{document}

\hypplot{-1.96}{-1.08}

\end{document}

I suppose I can always just copy paste and change each of them, but wouldn't it be nice to have a command to do it?

2

Of course it would be nice. And if you copy and paste the code from the \hypplot comand and only replace the #1 and #2 to -1.96 and -1.08 you still get an error.

The problem is that PFGPlots does not accept expressions when parsing a coordinate (see this question about it) so if you just parse the expressions with \pgfmathparse and then pass the results with \pgfmathresult whenever you had an expression inside a coordinate it works just fine:

MWE

\documentclass{article}

\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{xparse}
\pgfplotsset{compat=1.14}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

%zc,zt
\NewDocumentCommand{\hypplot}{ m m }
{%
\begin{tikzpicture}
  \begin{axis}[ no markers, domain=-4:4, 
                samples=100, axis lines*=left,
                every axis x label/.style={at={(current axis.right of origin)}, anchor=west},
                height=5cm, width=12cm, 
                ytick=\empty, xtick=\empty,
                enlargelimits=false, clip=false, axis on top,
                y axis line style={draw=none}
                ]
    %%Shade the area for alpha
    \addplot [fill=cyan!20, draw=none, domain=-4:#1] {gauss(0,1)} \closedcycle; 

    %%Shade the area for p-value
    \addplot [fill=orange!20, draw=none, domain=#1:#2] {gauss(0,1)} \closedcycle; 

    %%draw the gaussian
    \addplot [very thick,cyan!50!black] {gauss(0,1)};

    %%draws a double-headed arrow for Zc
    \draw [latex-latex] (axis cs:#1,-0.07) -- node [fill=white] {$\displaystyle \frac{\sigma}{\sqrt{n}}$} (axis cs:0,0.-0.07);

    %%draws a double-headed arrow for Zt
    \draw [yshift=-0.6cm, latex-latex] (axis cs:#2,0.16) -- (axis cs:0,0.16);

    %%Label Zt
    \pgfmathparse{#1+#2)/2}
    \draw [yshift=-0.6cm](axis cs:(\pgfmathresult,0.22) node {$\displaystyle \frac{\sigma}{\sqrt{n}}$};

    %%label the mean
    \draw (axis cs:0,.43) node {$\mu=\mu_0^{}$};
    \addplot +[mark=none] coordinates {(0, 0) (0, 0.3989)};

    %%add alpha
    \pgfmathparse{#1*1.071}
    \draw (axis cs:\pgfmathresult,0.02) node {$\alpha$};  
    \addplot +[mark=none] coordinates {(#1, -.01) (#1, 0.08)};
    \pgfmathparse{#1*1.02}
    \draw (axis cs:\pgfmathresult,0.10) node {$\bar{x}_c^{}$};

    %add p-value border
    \pgfmathparse{#2*1.02}
    \addplot +[mark=none] coordinates {(#2, -.01) (#2, 0.22)};
    \draw (axis cs:\pgfmathresult,0.25) node {$\bar{x}$};
  \end{axis}
\end{tikzpicture}%
}

\begin{document}
\hypplot{-1.96}{-1.08}
\end{document}

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.