Ausgleichsgerade mit pgfplot

Tabellen und Grafiken erstellen und anordnen


Lodovico
Forum-Anfänger
Forum-Anfänger
Beiträge: 20
Registriert: Mi 27. Feb 2013, 01:00

Ausgleichsgerade mit pgfplot

Beitrag von Lodovico »

Hallo Zusammen.

Ich würde gerne mit pgfplots Messpunkte inklusive Fehlerbalken auftragen und dann automatisch einen linearen Fit hindurchlegen. Die Messpunkte und Fehlerbalken bekomme ich hin, der zweite Plotbefehl ist allerdings ein verzweifelter Versuch, eine Regressionsgerade durch die Messpunkte zu zeichnen. Kann mit jemand auf die Sprünge helfen? Im Netz finde ich leider kein Beispiel, in dem die Regression mit index-Anweisungen realisiert wird. Danke!
\begin{tikzpicture}
		\begin{axis}[%
			width=.9\linewidth,
			height=.5\linewidth,
			axis x line*=box,
			axis y line*=box,
			xtick pos=left,
			ytick pos=left,
			tick align=outside,
			%xmin=-0.5,xmax=5.5,
			xlabel=Massenanteil $\longrightarrow$,
			ylabel=$A_{Pb}/A_{Cr}$ $\longrightarrow$,
			%legend pos=outer north east,
			%legend cell align=right
			]
			\addplot+[only marks,error bars/.cd,y dir=both,y explicit]%
		      	table[x index=0,y index=1,y error index=2,header=false]%
				{./Daten/2013-07-22/Kennlinie.asc};
			\addplot[red] table[x index=0,y create col/linear regression index=1]%
				{./Daten/2013-07-22/Kennlinie.asc};
		\end{axis}
\end{tikzpicture}

jupiter4065139

Beitrag von jupiter4065139 »

schau mal hier:
http://www.dante.de/events/Archiv/herbs ... folien.pdf

da gibts zum schluss was mit regression

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Bitte erstelle immer ein lauffähiges Minimalbeispiel. Hast du das Kapitel zur Regression in der Doku von pgfplots gelesen? Da steht auch wie diese mit index Angaben funktioniert
\documentclass[margin=5mm]{standalone} 
\usepackage{pgfplots}
\usepackage{pgfplotstable} 
\pgfplotsset{compat=1.9}
\usepackage{filecontents} 

\begin{filecontents}{plotdata.dat} 
 -3.5 0.350783228 -0.343633445 
 -3.25 0.108195135 -0.107984166 
 -3 -0.141120008 0.140652077 
 -2.75 -0.381660992 0.372462462 
 -2.5 -0.598472144 0.563380821 
 -2.25 -0.778073197 0.701908324 
\end{filecontents} 
\begin{document}
\begin{tikzpicture} 
\begin{axis}
  \addplot[red] table [x index=0,y={create col/linear regression={y=[index]1}}] 
    {plotdata.dat}; 
\end{axis} 
\end{tikzpicture}
\end{document}
Gruß
Elke

Lodovico
Forum-Anfänger
Forum-Anfänger
Beiträge: 20
Registriert: Mi 27. Feb 2013, 01:00

Beitrag von Lodovico »

Hallo Elke.

Sorry und vielen Dank. Könntest du mir vielleicht noch einen Tip geben und mit sagen, wie ich das x-Intervall modifizieren kann. Im Moment verdindet er nur den ersten mit dem letzten Messpunkt. Mit domain hab ich schon getestet, hat aber leider keinen Einfluss. Danke dir!

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Bei mir werden hier zum Beispiel nicht einfach der erste und der letzte Punkt verbunden:
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots} 
\usepackage{pgfplotstable} 
\pgfplotsset{compat=1.9} 
\usepackage{filecontents} 

\begin{filecontents}{plotdata.dat} 
  -3.5 0.5
  -3.25 0.1
  -3 -0.12
  -2.75 -0.4
  -2.5 -0.6
  -2.25 -0.5 
\end{filecontents} 

\begin{document} 
\begin{tikzpicture} 
\begin{axis} 
   \addplot+[blue] table [x index=0,y index=1]{plotdata.dat}; 
   \addplot+[red] table [x index=0,y={create col/linear regression={y=[index]1}}] 
     {plotdata.dat}; 
\end{axis} 
\end{tikzpicture} 
\end{document}
Mit skip coords between index={}{} oder so etwas wie restrict x to domain=-3:-2.5 lässt sich der dargestellte Bereich zwar einschränken. In beiden Fällen fließen aber alle Werte aus der dafür angegebenen Tabelle in die Regression ein.

Gruß
Elke
Zuletzt geändert von esdd am Do 14. Nov 2013, 10:33, insgesamt 1-mal geändert.

Lodovico
Forum-Anfänger
Forum-Anfänger
Beiträge: 20
Registriert: Mi 27. Feb 2013, 01:00

Beitrag von Lodovico »

Hallo Elke.

Das ist völlig korrekt. Ich möchte allerings, dass die Regressionsgerade in einem angegeben x-Bereich, z.B. von 0 bis 10 geplottet wird. Danke!

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Dann nimm restrict x to domain=<anfangswert>:<endwert>, also in meinem Beispiel etwas wie:
\addplot+[red,restrict x to domain=-3:-2.5] table [x index=0,y={create col/linear regression={y=[index]1}}]
Gruß
Elke

Lodovico
Forum-Anfänger
Forum-Anfänger
Beiträge: 20
Registriert: Mi 27. Feb 2013, 01:00

Beitrag von Lodovico »

Eben nicht. Hier mein konkretes Beispiel. Mit
restrict x to domain=0:4
werden zwar alle Werte zwischen 0 und 4 einbezogen, die Regression wird aber nur zwischen dem ersten und letzten Messpunkt geplottet. Was ich aber möchte, ist wie im Fall der extern erstellen Regressionsgerade (grün), dass die Regression von 0 bis 4 geplottet wird. Ich hoffe, ich konnte mich verständlich machen. Danke!
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{Daten.dat}
	1.16066180e+00 2.28442625e-01 4.32300081e-02
	1.73802471e+00 2.91068472e-01 7.29760080e-02
	2.29618963e+00 5.17043996e-01 2.65684424e-01
	3.29570747e+00 6.41803099e-01 5.03999200e-01
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}
   \addplot+[only marks,error bars/.cd,y dir=both,y explicit]%
		table[x index=0,y index=1,y error index=2]%
		{Daten.dat};
   \addplot+[green,mark=none,domain=0:4]{-0.017770+0.206044*x};
   \addplot+[red,mark=none,restrict x to domain=0:4]%
   	table [x index=0,y={create col/linear regression={y=[index]1}}]%
   	{Daten.dat};
\end{axis}
\end{tikzpicture}
\end{document}

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

Du möchtest den gezeichneten Bereich also nicht einschränken sondern die Gerade verlängern. Das hast du uns bisher nicht verraten und ein Beispiel an dem man das hätte erkennen können, gab es auch nicht.

Wenn die Gerade nur etwas verlängert werden soll, könnte man das so lösen:
\documentclass[margin=5mm]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}

\usepackage{filecontents} 
\begin{filecontents}{Daten.dat} 
    1.16066180e+00 2.28442625e-01 4.32300081e-02 
    1.73802471e+00 2.91068472e-01 7.29760080e-02 
    2.29618963e+00 5.17043996e-01 2.65684424e-01 
    3.29570747e+00 6.41803099e-01 5.03999200e-01 
\end{filecontents} 

\begin{document} 
\begin{tikzpicture} 
\begin{axis}[xmin=-0.5,xmax=4.5,ymin=-0.1] 
    \addplot+[only marks,error bars/.cd,y dir=both,y explicit]% 
       table[x index=0,y index=1,y error index=2]% 
       {Daten.dat}; 
    %\addplot+[green,mark=none,domain=0:4]{-0.017770+0.206044*x}; 
    \addplot+[draw=none,mark=none]% 
       table [x index=0,y={create col/linear regression={y=[index]1}}]% 
       {Daten.dat}coordinate[pos=0](a)coordinate[pos=1](e);
    \draw[red] ($(a)!-0.3!(e)$)--($(a)!1.3!(e)$);
\end{axis} 
\end{tikzpicture} 
\end{document}
Hier ist noch ein Vorschlag, falls die Gerade genau von 0 bis 4 gehen soll:
\documentclass[margin=5mm]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}

\usepackage{filecontents} 

\begin{filecontents}{Daten.dat} 
    1.16066180e+00 2.28442625e-01 4.32300081e-02 
    1.73802471e+00 2.91068472e-01 7.29760080e-02 
    2.29618963e+00 5.17043996e-01 2.65684424e-01 
    3.29570747e+00 6.41803099e-01 5.03999200e-01 
\end{filecontents} 

\begin{document} 
\begin{tikzpicture}
\newcommand\xl{0}
\newcommand\xr{4}
\begin{axis}[xmin=-0.5,xmax=4.5,ymin=-0.1]
    \addplot+[only marks,error bars/.cd,y dir=both,y explicit]% 
       table[x index=0,y index=1,y error index=2]% 
       {Daten.dat}; 
    %\addplot+[green,mark=none,domain=0:4]{-0.017770+0.206044*x}; 
    \addplot+[draw=none,mark=none]% 
       table [x index=0,y={create col/linear regression={y=[index]1}}]% 
       {Daten.dat}coordinate[pos=0](a)coordinate[pos=1](e);
    \coordinate(l)at({axis cs:\xl,0}|-{rel axis cs:0,0});
    \coordinate(r)at({axis cs:\xr,0}|-{rel axis cs:0,1});
\end{axis}
    \clip (l)rectangle(r);
    \draw[red] ($(a)!-2!(e)$)--($(a)!2!(e)$);
\end{tikzpicture} 
\end{document}
Gegebenenfalls musst du mit xmin, xmax, ymin, ymax dafür sorgen, dass dein Koordinatensystem groß genug für die verlängerte Gerade ist.

Gruß
Elke

Lodovico
Forum-Anfänger
Forum-Anfänger
Beiträge: 20
Registriert: Mi 27. Feb 2013, 01:00

Beitrag von Lodovico »

Hallo Elke.

Vielen Dank für deine Ansätze. Wieder viel gelernt. Ich hab das Problem jetzt aus meiner Sicht etwas stringenter mithilfe von pgfplotstable gelöst.
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{Daten.dat}
	1.16066180e+00 2.28442625e-01 4.32300081e-02
	1.73802471e+00 2.91068472e-01 7.29760080e-02
	2.29618963e+00 5.17043996e-01 2.65684424e-01
	3.29570747e+00 6.41803099e-01 5.03999200e-01
\end{filecontents}

\begin{document}

\pgfplotstableread[columns={[index]0,[index]1}]{Daten.dat}\daten
\pgfplotstablecreatecol[linear regression]{regression}{\daten}
\xdef\slope{\pgfplotstableregressiona}
\xdef\intercept{\pgfplotstableregressionb}

\begin{tikzpicture}
\begin{axis}
   \addplot+[only marks,error bars/.cd,y dir=both,y explicit]%
		table[x index=0,y index=1,y error index=2]%
		{Daten.dat};
   \addplot[red,no markers,domain=0:4] {\intercept+\slope*x};
\end{axis}
\end{tikzpicture}
\end{document}

Antworten