Seite 1 von 1

Tikzpicture wird plötzlich nicht mehr generriert

Verfasst: Di 13. Jun 2017, 12:57
von ghostify24
Hi, Forum Gemeinde, hätte da ein kleines Problem mit der Erstellung eines Grafen mit Tikzpictures. Bis vor kurzem hat es auch funktioniert leider heute morgen nicht mehr, hmmm. Bin leider auch etwas im Stress, Thesis muss langsam zum Ende kommen. Vielleicht sieht ja jemand den Fehler, der sich im Eifer des Gefechtes eingeschlichen hat.

Danke schon mal. :D
\documentclass{article} 
\usepackage{tikz,pgfplots}

\pgfplotsset{compat=1.14}

% Gauss function, parameters mu and sigma
% #1 is \mu; #2 is \sigma
\pgfmathdeclarefunction{gaussian}{2}{                                           %exp(\frac{(x-\mu)^2}{2*\sigma^2})
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}           %\sigma*\sqrt{2*pi}


\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[no markers, domain=97.8:102.2, samples=250, axis lines=left, xlabel=$x$, ylabel=$f_{X}(x)$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=6cm, width=12cm, ymin=0, ymax=1.5,
ticks=none,                                    %Ausblenden der Achsenskalierung, auskommentieren falls Skalen erwünscht
ytick={0.25,0.50,0.75,0.90,1},
xtick={97,97.5,98,98.5,99,99.5,100,100.5,101,101.5,102,102.5,103,103.5},
enlargelimits=false, clip=false, axis on top]
\addplot [very thick,cyan!50!black] {gaussian(100,0.8};    % #1 is \mu; #2 is \sigma^2
\addplot [very thick,cyan!50!black] {gaussian(100,0.5)};
\addplot [very thick,cyan!50!black] {gaussian(100,0.3)};
%\draw (axis cs: 100,-.1)   node [fill=none] {$\mu$};
\draw [<-](axis cs: 100.30,1.1) -- (axis cs: 100.60,1.4)  node [fill=white] {$\sigma_1$};
\draw [<-](axis cs: 100.65,0.5) -- (axis cs: 101.05,0.9)  node [fill=white] {$\sigma_2$};
\draw [<-](axis cs: 101.25,0.2) -- (axis cs: 101.65,0.6)  node [fill=white] {$\sigma_3$};
\draw (axis cs: 101.5,1.45) node [fill=none] {$\sigma_1<\,\sigma_2<\,\sigma_3$};
\draw [dotted](axis cs: 100,0) -- (axis cs: 100,1.3);
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Verfasst: Di 13. Jun 2017, 17:03
von esdd
Das Problem ist die Definition der Funktion. Du hast in dieser ein Zeilenende, das jeweils ein Leerzeichen einfügt. Bei der hohen sample Zahl kommt da so viele zusammen, dass die Zeichnung nach rechts aus dem Papier rauswandert. Fügst Du am Ende der Zeile ein Kommentarzeichen ein, dann kommentierst Du das Zeilenende-Leerzeichen aus und der Plot ist wieder da:
% Gauss function, parameters mu and sigma
% #1 is \mu; #2 is \sigma
\pgfmathdeclarefunction{gaussian}{2}{%<- das Kommentarzeichen muss hier sein!!
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}
Vollständiges Bespiel:
\documentclass{article} 
\usepackage{pgfplots}% lädt auch tikz
\pgfplotsset{compat=1.14}

% Gauss function, parameters mu and sigma
% #1 is \mu; #2 is \sigma
\pgfmathdeclarefunction{gaussian}{2}{%<- das Kommentarzeichen muss hier sein!!
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}

\begin{document}
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[no markers,domain=97.8:102.2,samples=250,
      axis lines=left, xlabel=$x$, ylabel=$f_{X}(x)$,
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      height=6cm, width=12cm, ymin=0, ymax=1.5,
      ticks=none,%Ausblenden der Achsenskalierung, auskommentieren falls Skalen erwünscht
      ytick={0.25,0.50,0.75,0.90,1},
      xtick={97,97.5,98,98.5,99,99.5,100,100.5,101,101.5,102,102.5,103,103.5},
      enlargelimits=false,
      clip=false,
      axis on top
    ]
      \addplot [very thick,cyan!50!black] {gaussian(100,0.8)};    % #1 is \mu; #2 is \sigma^2
      \addplot [very thick,cyan!50!black] {gaussian(100,0.5)};
      \addplot [very thick,cyan!50!black] {gaussian(100,0.3)};
      %\draw (100,-.1) node [fill=none] {$\mu$};
      \draw [<-](100.30,1.1) -- (100.60,1.4)  node [fill=white] {$\sigma_1$};
      \draw [<-](100.65,0.5) -- (101.05,0.9)  node [fill=white] {$\sigma_2$};
      \draw [<-](101.25,0.2) -- (101.65,0.6)  node [fill=white] {$\sigma_3$};
      \draw (101.5,1.45) node [fill=none] {$\sigma_1<\,\sigma_2<\,\sigma_3$};
      \draw [dotted](100,0) -- (100,1.3);
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{document}
Edit: Fehlende schließende Klammer für gaussian(100,0.8) eingefügt.

Verfasst: Di 13. Jun 2017, 17:36
von Bartman
@ghostify24

Am Ende von gaussian(100,0.8 fehlt die schließende runde Klammer. Deren Abwesenheit scheint aber keine sichtbare Veränderung zu bewirken.

Verfasst: Di 13. Jun 2017, 17:58
von esdd
Bartman hat geschrieben: Am Ende von gaussian(100,0.8 fehlt die schließende runde Klammer. Deren Abwesenheit scheint aber keine sichtbare Veränderung zu bewirken.
Danke. Die fehlende Klammer habe ich übersehen, jetzt aber oben noch ergänzt.

Verfasst: Di 13. Jun 2017, 18:29
von ghostify24
Danke sehr, ihr seid einfach spitze.
Wäre gar nicht darauf gekommen, haha.Was neues dazu gelernt. :D

Verfasst: Mi 14. Jun 2017, 00:39
von Bartman
Ein paar Anpassungen des Beispiels konnte ich mir nicht verkneifen.
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}% lädt auch tikz
\pgfplotsset{compat=1.14}% inzwischen ist 1.15 verfügbar

% Gauss function, parameters mu and sigma
% #1 is \mu; #2 is \sigma
\pgfmathdeclarefunction{gaussian}{2}{%<- das Kommentarzeichen muss hier sein!!
	\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
	no markers,
	domain=97.8:102.2,
	samples=250,
	axis lines=left, xlabel=$x$, ylabel=$f_X(x)$,
	every axis y label/.style={at=(current axis.above origin),anchor=south},
	every axis x label/.style={at=(current axis.right of origin),anchor=west},
	height=6cm, width=12cm, ymin=0, ymax=1.5,
	ticks=none,% Ausblenden der Achsenskalierung, auskommentieren falls Skalen erwünscht
	ytick={0.25,0.50,0.75,0.90,1},
	xtick={97,97.5,...,103.5},
	enlargelimits=false,
	clip=false,
	axis on top,
	curve/.style={very thick,cyan!50!black}
]
\addplot [curve] {gaussian(100,0.8)} coordinate [pos=.77] (sigma3);% #1 is \mu; #2 is \sigma^2
\addplot [curve] {gaussian(100,0.5)} coordinate [pos=.64] (sigma2);
\addplot [curve] {gaussian(100,0.3)} 
	coordinate [pos=.56] (sigma1)
	coordinate [midway] (sigma1midpoint);

% Kurvenbeschriftung
\begin{scope}[
	<-,
	nodes={above right},
	arrowhead/.style={above right=##1},
	arrowhead/.default=2mm,
	arrowline/.style={arrowhead=7mm, yshift=1mm},
]
\pgfplotsinvokeforeach{3,2,1}{
	\draw ([arrowhead]sigma#1) -- ([arrowline]sigma#1) 
		node {$\sigma_#1$};
}
\end{scope}

\draw ([shift={(-16.5mm,-1.5mm)}]current axis.north east) 
	node {$\sigma_1<\sigma_2<\sigma_3$};
\draw [dotted] (sigma1midpoint) -- (sigma1midpoint |- current axis.origin) 
	node [below] {$\mu$};
\end{axis}
\end{tikzpicture}
\end{document}
@ghostify24

Meine Erwähnung der neueren Version von pgfplots soll nur ein Hinweis sein. Zur Sicherheit solltest Du vor einem nahenden Abgabetermin besser kein Update mehr anstreben.

Bearbeitung: Ich habe die Angabe des Koordinatenursprungs von esdd übernommen.

Verfasst: Mi 14. Jun 2017, 07:13
von esdd
Man kann die Beschriftung auch als pin einfügen:
\documentclass{article} 
\usepackage{pgfplots}% lädt auch tikz
\pgfplotsset{compat=1.14}

% Gauss function, parameters mu and sigma
% #1 is \mu; #2 is \sigma
\pgfmathdeclarefunction{gaussian}{2}{%<- das Kommentarzeichen muss hier sein!!
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}

\begin{document}
\begin{center}
  \begin{tikzpicture}[
  	arrowline/.style={pin={[text=black,pin edge={<-,shorten <=1mm,black},pin distance=10mm]56:#1}}
  ]
    \begin{axis}[no markers,domain=97.8:102.2,samples=250,
      axis lines=left, xlabel=$x$, ylabel=$f_{X}(x)$,
      every axis y label/.style={at=(current axis.above origin),anchor=south},
      every axis x label/.style={at=(current axis.right of origin),anchor=west},
      height=6cm, width=12cm, ymin=0, ymax=1.5,
      ticks=none,%Ausblenden der Achsenskalierung, auskommentieren falls Skalen erwünscht
      ytick={0.25,0.50,0.75,0.90,1},
      xtick={97,97.5,...,103.5},
      enlargelimits=false,
      clip=false,
      axis on top,
      title style={at={(.96,.975)},left},
      title=$\sigma_1<\,\sigma_2<\,\sigma_3$,
    ]
      \addplot [very thick,cyan!50!black] {gaussian(100,0.8)} node[pos=.78,arrowline=$\sigma_3$]{};    
      \addplot [very thick,cyan!50!black] {gaussian(100,0.5)} node[pos=.63,arrowline=$\sigma_2$]{};
      \addplot [very thick,cyan!50!black] {gaussian(100,0.3)} node[pos=.57,arrowline=$\sigma_1$]{}
        coordinate [midway] (sigma1midpoint);
      \draw [dotted](sigma1midpoint) -- (sigma1midpoint|-current axis.origin)node [below] {$\mu$};
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{document}

Verfasst: Mi 14. Jun 2017, 09:47
von ghostify24
Vielen Dank, da scheint es ja einige tolle mir noch unbekannte Funktionen zu geben. :D