4

I wanna plot the density of a normal distribution as it is in the pic below. The Problems:

  • the node t=1,63 does not appear in the plot
  • do not know how to draw the arrow in an easy way

Here's my code so far:

\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{ntheorem}
\usepackage{here}
\usepackage{multirow}
\usepackage{eurosym}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,shadows}
\pagestyle{empty}
\usepackage{graphicx}
\usepackage{array}
\usetikzlibrary{decorations.pathreplacing} 
\usepackage{colortbl}
\usepackage[ngerman]{babel}
\usepackage{geometry}
\geometry{a4paper, top=15mm, left=25mm, right=25mm, bottom=20mm,
 headsep=10mm, footskip=12mm}
\theoremstyle{break}
\usepackage{amsfonts}
\usepackage{color}  
\usepackage{gauss}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{gensymb}
\usepackage{systeme}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\usepackage{booktabs}
\usetikzlibrary{positioning,fit,calc}
\usetikzlibrary{backgrounds}
\usepackage{amssymb}
\usepackage{amsmath}
\addtokomafont{caption}{\footnotesize}
\setlength{\mathindent}{0pt}
\makeatletter
\g@addto@macro\normalsize{%  <----
 \setlength\abovedisplayskip{0pt}%  <----
 \setlength\belowdisplayskip{10pt}%  <----
 \setlength\abovedisplayshortskip{0pt}%  <----
\setlength\belowdisplayshortskip{20pt}%  <----
}
 \makeatother
 \setlength{\intextsep}{0pt}
  \tikzset{declare function={
    normcdf(\x,\m,\s)=1/(1 + exp(-0.07056*((\x-\m)/\s)^3 - 1.5976*     (\x-\m)/\s));
}}
       \pgfmathdeclarefunction{gauss{2}{%\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%}

\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} % linksbündig mit Breitenangabe
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} % zentriert mit Breitenangabe
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}} % rechtsbündig mit Breitenangabe

\newcommand*\xbar[1]{%
 \hbox{%
  \vbox{%
  \hrule height 0.5pt % The actual bar
  \kern0.5ex%         % Distance between bar and symbol
  \hbox{%
    \kern-0.1em%      % Shortening on the left side
    \ensuremath{#1}%
    \kern-0.1em%      % Shortening on the right side}%}%}%}

 \usepackage{etoolbox}
\makeatletter
\patchcmd\g@matrix
 {\vbox\bgroup}
 {\vbox\bgroup\normalbaselines}% restore the standard baselineskip
{}{}
\makeatother


 \newcommand{\BAR}{%
  \hspace{-\arraycolsep}%
  \strut\vrule % the `\vrule` is as high and deep as a strut
 \hspace{-\arraycolsep}%
  }

    \usepackage{tabstackengine}[2016-10-04]
  \stackMath


\begin{document}
\begin{figure}[H]
    \centering
\begin{tikzpicture}
\begin{axis}[ domain=-1:1,
         scale only axis,
         axis x line=middle,
         axis y line=middle,
         inner axis line style={=>},
                 width=15cm,height=6cm,
                ymin=0,ymax=0.45,
                xmin=-3.5,xmax=3.5,
                axis line style = thick,
                xtick={-3,-2,-1,1,2,3},
                ytick={0.1,0.2,0.3,0.4},
        every axis x label/.style={at={(current axis.right of origin)},anchor=west},
         every axis y label/.style={at={(current axis.north)},above=0.5mm},
                 xlabel={$x$},
                 ylabel={$f(x)$},
                    axis on top]
      \addplot[fill=blue!25,draw=none,domain=-3.5:-1.63] {gauss(0,1)}     \closedcycle;]
\addplot[fill=blue!25,draw=none,domain=1.63:3.5] {gauss(0,1)} \closedcycle;]
\addplot [domain=-3.5:3.5,samples=500,red,thick] {gauss(0,1)};
\node at (axis cs:1.63,-0.05){$t=1.63$};
\end{axis}
\end{tikzpicture}
\caption{Graph der Dichtefunktion $f_X(x)$ mit $X\sim\mathcal{N}(0,1)$}%
\end{figure}
 \end{document}

enter image description here

Stefan Pinnow
  • 29,535
user150752
  • 155
  • 2
  • 11

1 Answers1

5

like this?

enter image description here

note: for present of your Gaussian the most of preamble in your minimal working example is superfluous. i omit all what is not needed for present it.

edit: after reinspected your image i found that extra x tick label $\stackrel{\uparrow}{t=1.63}$ is not logical. abscissa is labeld x, so it should be sufficient to label this point only with number. if you like to emphasize it, you can change its color or face/shape. corrected.

\documentclass[a4paper,11pt,fleqn]{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
                                }
\usetikzlibrary{arrows.meta}    % <--- added

\begin{document}
    \begin{figure}[htb]
    \centering
\begin{tikzpicture}[
    every pin/.style = {pin edge={Latex-,thin,black}}
                    ]
\begin{axis}[
    width=15cm,height=6cm,
        scale only axis,
        axis lines=middle,
        ymin=0,ymax=0.45,
        axis line style = thick,
        xtick={-3,-2,-1,1,2,3},
        extra x ticks ={1.63},
        extra x tick labels ={\color{blue}1.63},
        x label style={anchor=west},
        y label style={anchor=south},
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top,
        samples=50]
\addplot[fill=blue!25,draw=none,domain=-3.5:-1.63] {gauss(0,1)} \closedcycle;
\addplot[fill=blue!25,draw=none,domain=1.63:3.5]   {gauss(0,1)} \closedcycle;
\addplot[domain=-3.5:3.5,red,thick] {gauss(0,1)};
\node [pin= 60:$P_{val}/2$] at ( 2,0.02)   {};
\node [pin=120:$P_{val}/2$] at (-2,0.02)   {};
\end{axis}
\end{tikzpicture}
\caption{Graph der Dichtefunktion $f_X(x)$ mit $X\sim\mathcal{N}(0,1)$}%
    \end{figure}
\end{document}

edit: since you insist in your arrow and labels ...

\documentclass[a4paper,11pt,fleqn]{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
                                }
\usetikzlibrary{arrows.meta}    % <--- added

\begin{document}
    \begin{figure}[htb]
    \centering
\begin{tikzpicture}[
    every pin/.style = {pin edge={Latex-,thin,black}}
                    ]
\begin{axis}[
    width=15cm,height=6cm,
        scale only axis,
        axis lines=middle,
        ymin=0,ymax=0.45,
        axis line style = thick,
        xtick={-3,-2,-1,1,2,3},
        x label style={anchor=west},
        y label style={anchor=south},
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top,
        samples=50,
        clip=false]
\addplot[fill=blue!25,draw=none,domain=-3.5:-1.63] {gauss(0,1)} \closedcycle;
\addplot[fill=blue!25,draw=none,domain=1.63:3.5]   {gauss(0,1)} \closedcycle;
\addplot[domain=-3.5:3.5,red,thick] {gauss(0,1)};
\node [pin= 60:$P_{val}/2$] at ( 2,0.02)   {};
\node [pin=120:$P_{val}/2$] at (-2,0.02)   {};
\node [below,pin=below:{$t=1.63$}] at (1.63,0.01)   {};
\end{axis}
\end{tikzpicture}
\caption{Graph der Dichtefunktion $f_X(x)$ mit $X\sim\mathcal{N}(0,1)$}%
    \end{figure}
\end{document}

enter image description here

edit (2): a variation of above image code. if you replace

\node [pin= 60:$P_{val}/2$] at ( 2,0.02)   {};
\node [pin=120:$P_{val}/2$] at (-2,0.02)   {};
\node [below,pin=below:{$t=1.63$}] at (1.63,0.01)   {};

with

\coordinate[pin= 60:$P_{val}/2$] (x) at ( 2,0.02);
\coordinate[pin=120:$P_{val}/2$] (x) at (-2,0.02);
\coordinate[pin=below:{$t=1.63$}](x) at ( 1.63,0);

you will obtain:

enter image description here

Zarko
  • 296,517
  • Thank you very much. The node with the p_{val}/2 looks great. Does there exist an similiar solution for the node under the x-axis? The same type of arrow and the text a little bit further down would be great – user150752 Dec 16 '17 at 12:27
  • @Eric, i slightly change my answer. – Zarko Dec 16 '17 at 13:27
  • Looks good. But is there no way to put an arrow between the node and the axis like it is by p values? – user150752 Dec 16 '17 at 14:04
  • @Eric, yes there is always a way ... see edit in answer, however i will stick with first solution. all is up to you. – Zarko Dec 16 '17 at 16:53
  • @Eric, if answer solve all your requirements, you can vote and accept it :-) /by clicking on the upper pen and check mark at the top left side of my answer/. – Zarko Dec 17 '17 at 20:43
  • @Eric, any news? – Zarko Dec 18 '17 at 09:08