
Por favor indique cómo ajustar a y b usando los valores de las columnas X e Y en el siguiente código
\documentclass{article}
\usepackage{gnuplot-lua-tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{\gpbboxborder}
\begin{document}
\begin{tikzpicture}[gnuplot]
\begin{axis}[
width=10cm,
xlabel={something},
ylabel={something},
]
\addplot [blue,only marks] table [x=X, y=Y] {
X Y
145444 3746643.308
145250 3972396.385
145183 5026818
145449 8220037.462
145355 7696519.385
145347 7952214.231
145394 5869103.66
145421 7268796.583
145249 8472536.083
145237 9305086.167
145358 5155636.25
145416 6077647.583
145337 5861633.167
145384 4426391.667
145229 4101591.378
145392 4570673.462
145271 6972571.692
145287 6527322.308
145319 4211914.846
145321 4735368.385
145411 7849477.75
145349 8152333.083
145233 4891463
145261 5013476.583
145424 4030219.154
145381 4528078.154
145326 5445707.231
145413 5461362.231
145311 5619711.846
145409 9969929.462
145248 5306496.923
145270 7.53E+06
145281 4478472.083
145341 5173546.667
};
{f(x)};
\addplot [thick,red,dashed,id=test] gnuplot [raw gnuplot] {
X Y
145444 3746643.308
145250 3972396.385
145183 5026818
145449 8220037.462
145355 7696519.385
145347 7952214.231
145394 5869103.66
145421 7268796.583
145249 8472536.083
145237 9305086.167
145358 5155636.25
145416 6077647.583
145337 5861633.167
145384 4426391.667
145229 4101591.378
145392 4570673.462
145271 6972571.692
145287 6527322.308
145319 4211914.846
145321 4735368.385
145411 7849477.75
145349 8152333.083
145233 4891463
145261 5013476.583
145424 4030219.154
145381 4528078.154
145326 5445707.231
145413 5461362.231
145311 5619711.846
145409 9969929.462
145248 5306496.923
145270 7.53E+06
145281 4478472.083
145341 5173546.667
f(x) = a*x + b;
a = 2000;
b = -3e8;
% fit a and b by `using' columns 1 and 2 of 'data.txt'
fit f(x) 'X Y' using 1:2 via a, b;
straight line
set samples 2;
plot [x=145183:145449] f(x);
};
\end{axis}
\end{tikzpicture}
\end{document}
Loop podría ser una solución, pero no pude entender cómo ejecutarlo.
No se pueden definir los valores en un archivo .text en el preámbulo porque es necesario agregar varios gráficos en un documento.
Respuesta1
pgfplots
junto con pgfplotstable
se puede utilizar para la regresión lineal. El valor de los coeficientes de regresión se almacenará en \pgfplotstableregressiona
y \pgfplotstableregressionb
.
Consulte el código que se proporciona a continuación. Tenga en cuenta que los datos se ordenaron y guardaron \res
antes de aplicar la regresión.
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={something},
ylabel={something},
]
\pgfplotstablesort{\res}{
X Y
145444 3746643.308
145250 3972396.385
145183 5026818
145449 8220037.462
145355 7696519.385
145347 7952214.231
145394 5869103.66
145421 7268796.583
145249 8472536.083
145237 9305086.167
145358 5155636.25
145416 6077647.583
145337 5861633.167
145384 4426391.667
145229 4101591.378
145392 4570673.462
145271 6972571.692
145287 6527322.308
145319 4211914.846
145321 4735368.385
145411 7849477.75
145349 8152333.083
145233 4891463
145261 5013476.583
145424 4030219.154
145381 4528078.154
145326 5445707.231
145413 5461362.231
145311 5619711.846
145409 9969929.462
145248 5306496.923
145270 7.53E+06
145281 4478472.083
145341 5173546.667
};
{f(x)};
\addplot [blue,only marks] table [sort, sort key=X, x=X, y=Y] {\res};
\addplot [dashed,red,thick] table [x=X, y={create col/linear regression={y=Y}}] {\res};
\addlegendentry {data}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \times x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}