我正在尝试包括斜率的标准误差作为
“斜率 +- 误差...> 1.65 +- 0.017”
并且线拟合代替
标记=x
在对数对数图上。但我无法计算误差并显示 lint 拟合而不是mark=x
。请帮我做一下。
============================
**我的代码是:
\documentclass[border=5pt]{standalone}
\usepackage{xcolor}
\usepackage{tikz,pgfplots,pgfplotstable}
\begin{filecontents}{main-data.dat}
0 1
1 7
2 15
3 27
4 39
5 59
6 73
7 97
8 113
9 133
10 152
11 176
12 207
13 243
14 268
15 298
16 332
17 368
18 398
19 429
20 469
\end{filecontents}
\begin{filecontents}{slope-data.dat}
8 113
9 133
10 152
11 176
12 207
13 243
14 268
15 298
16 332
17 368
18 398
19 429
20 469
\end{filecontents}
\begin{document}
\centering
\pgfplotsset{every major tick/.style={black,},minor y tick num=1}
\pgfplotstableread{slope-data.dat}\datatable
%
\begin{tikzpicture}[scale=1.4]
\begin{loglogaxis}[
axis background/.style={
shade,top color=gray!60,bottom color=white},
legend style={fill=white,font=\scriptsize, at={(0.96,0.10)},anchor=south east},
tick label style={font=\scriptsize},
label style={font=\footnotesize},
legend cell align=left,
xlabel={$a$},
ylabel={$N(a)$},
legend entries={$r = 0.50-M=2^{0}$, $ d_\mathrm{f} =
\pgfmathprintnumber{\slope} \pm $},
]
%
\addplot+[mark=halfcircle*,only marks,every mark/.append style={rotate=90}]
table[font=\scriptsize] {main-data.dat};
%
\addplot+[mark=x,mark repeat=5,mark phase=6,smooth]
table[y={create col/linear regression={y=1}}]{\datatable}
coordinate [pos=0.3] (A)
coordinate [pos=0.69] (B);
% store slope
\xdef\slope{\pgfplotstableregressiona}
\draw [red,thick] (A) |- (B)
node[anchor=east,font=\scriptsize,red] at (3.33,7.8)
{$\sim$\pgfmathprintnumber{\slope}};
%
\end{loglogaxis}
\end{tikzpicture}
\end{document}
答案1
这更像是一条评论,而不是答案,但有点冗长......
LaTeX 实际上排版程序而不是计算程序。虽然您可以使用 LaTeX 和 Friends 完成这项任务,但我建议您在其他地方进行计算,并且只打印结果使用 LaTeX/TikZ/PGFPlots。
因此你可以使用以下方法执行以下操作格努普特:
% used PGFPlots v1.16
\begin{filecontents}{main-data.dat}
0 1
1 7
2 15
3 27
4 39
5 59
6 73
7 97
8 113
9 133
10 152
11 176
12 207
13 243
14 268
15 298
16 332
17 368
18 398
19 429
20 469
\end{filecontents}
\begin{filecontents}{slope-data.dat}
8 113
9 133
10 152
11 176
12 207
13 243
14 268
15 298
16 332
17 368
18 398
19 429
20 469
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={$a$},
ylabel={$N(a)$},
legend pos=south east,
legend cell align=left,
]
\addplot+ [
mark=halfcircle*,
only marks,
every mark/.append style={rotate=90},
] table {main-data.dat};
\addplot [
red,
% use gnuplot to calculate the parameters of the straight line
% including the corresponding error values
raw gnuplot,
] gnuplot {
% initialize variables
a = 1;
b = 1;
% define fit function
f(x) = a*x + b;
% fit variables using log-log values of table data
fit f(x) 'slope-data.dat' using (log($1)):(log($2)) via a,b;
% a (straight) line only needs two samples
set samples 2;
% plot the result
plot [x=8:20] exp(f(log(x)));
};
\legend{
$r = 0.50 - M = 2^{0}$,
$d_\mathrm{f} =
% add numbers given in 'fit.log'
\pgfmathprintnumber{1.58617}
\pm \pgfmathprintnumber[fixed]{0.0183}$,
}
\end{loglogaxis}
\end{tikzpicture}
\end{document}