Präzision eines Plots mit TikZ

Präzision eines Plots mit TikZ

Warum gehen die Linien über die Abszissenachse hinaus (blaue Kreise)?

   \documentclass[border=5mm]{standalone}
   \usepackage{pgfplots}
   \usepackage{amssymb}
   \begin{document}

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

   \begin{tikzpicture}
   \begin{axis}[
   domain=-3:3,
   tickwidth=0.1cm,
   major tick style=black,
   axis lines=middle,
   axis line style =thick,
   axis line style={shorten >=-10pt, shorten <=-15pt},
   xlabel=$x$,ylabel=$y$,
   xtick={-2,...,2},ytick={0,0.1,0.2,0.3},
   xticklabels={,,,1,,,},
   yticklabels={,0.1,,},
   height=5cm, width=10cm,
   clip=false,
   axis on top,
   every axis x label/.style={
   at={(ticklabel* cs:1.03)},
   anchor=west},
   every axis y label/.style={
   at={(ticklabel* cs:1.1)},
   anchor=south},
   samples=1000,
   >=stealth,
   ]

  \pgfmathsetmacro\valueA{gauss(0.8)}
  \pgfmathsetmacro\valueB{gauss(-1.5)}
  \addplot [fill=orange!40, draw=none, domain=-1.5:0.8] {gauss(x)} \closedcycle;
  \draw [orange!80,thick] (axis cs:0.8,0) -- (axis cs:0.8,\valueA);
  \draw [orange!80,thick] (axis cs:-1.5,0) -- (axis cs:-1.5,\valueB);
  \addplot [ thick] {gauss(x)};
  \node[below,orange!80] at (axis cs:0.8,0){$b$};
  \node[below,orange!80] at (axis cs:-1.5,0){$a$};
  \node[below left] at (axis cs:0,0){$0$};
  \draw[orange,->](axis cs:2,0.4)--(axis cs:0.2,0.25);
  \draw[blue] (axis cs:0.8,0)  circle [radius=0.2cm];
  \draw[blue] (axis cs:-1.5,0)  circle [radius=0.2cm];
  \node[draw][orange,anchor=south west] at (axis cs:2,0.4)  {$P(a\leqslant X \leqslant b)$};
  \end{axis}
  \end{tikzpicture}
  \end{document}

Bildbeschreibung hier eingeben

Antwort1

Ihr Problem hier ist, dass Ihre xAchse nicht durch den Ursprung verläuft(0,0)

Um das Problem zu beheben, stellen Sie es einfach yminauf 0oder weniger ein.

Die Ausgabe

Bildbeschreibung hier eingeben

Der Code

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{amssymb}
\begin{document}

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

\begin{tikzpicture}
  \begin{axis}
    [
      ymin=0, %%%%%%%%%%%%%%%%%%%%%%%%%%%  <--- just do this
      domain=-3:3,
      tickwidth=0.1cm,
      major tick style=black,
      axis lines=middle,
      axis line style =thick,
      axis line style={shorten >=-10pt, shorten <=-15pt},
      xlabel=$x$,ylabel=$y$,
      xtick={-2,...,2},ytick={0,0.1,0.2,0.3},
      xticklabels={,,,1,,,},
      yticklabels={,0.1,,},
      height=5cm, width=10cm,
      clip=false,
      axis on top,
      every axis x label/.style=
      {
        at={(ticklabel* cs:1.03)},
        anchor=west
      },
      every axis y label/.style=
      {
        at={(ticklabel* cs:1.1)},
        anchor=south
      },
      samples=100,
      >=stealth,
    ]

    \pgfmathsetmacro\valueA{gauss(0.8)}
    \pgfmathsetmacro\valueB{gauss(-1.5)}
    \addplot [fill=orange!40, draw=none, domain=-1.5:0.8] {gauss(x)} \closedcycle;
    \draw [orange!80,thick] (axis cs:0.8,0) -- (axis cs:0.8,\valueA);
    \draw [orange!80,thick] (axis cs:-1.5,0) -- (axis cs:-1.5,\valueB);
    \addplot [ thick] {gauss(x)};
    \node[below,orange!80] at (axis cs:0.8,0){$b$};
    \node[below,orange!80] at (axis cs:-1.5,0){$a$};
    \node[below left] at (axis cs:0,0){$0$};
    \draw[orange,->](axis cs:2,0.4)--(axis cs:0.2,0.25);
    \draw[blue] (axis cs:0.8,0)  circle [radius=0.2cm];
    \draw[blue] (axis cs:-1.5,0)  circle [radius=0.2cm];
    \node[draw][orange,anchor=south west] at (axis cs:2,0.4) {$P(a\leqslant X \leqslant b)$};
  \end{axis}
\end{tikzpicture}
\end{document}

Prost,

verwandte Informationen