我有这个情节
\begin{tikzpicture}
\pgfplotsset{%
,width=10cm
,legend style={font=\footnotesize}
,grid
}
\begin{axis}[enlargelimits=false
,ymax=130
,ymin=0
,xmax=9
,xmin=0]
]
\addplot+[
only marks,
]
table{tabel_3.dat};
\addlegendentry{Punctele masurate};
\addplot [blue]
gnuplot [raw gnuplot] {
f(x)=a*x+b;
a=0.0001;
b=0.0001;
fit f(x) 'tabel_3.dat' using 1:2 via a,b;
plot [x=0:12] f(x);
set print "parameters.dat";
print a, b;
};
\addlegendentry{\pgfplotstableread{parameters.dat}\parameters
\pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval}
\pgfplotstablegetelem{0}{1}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
$y=\pgfmathprintnumber{\paramA} \cdot x \pgfmathprintnumber[print sign]{\paramB} $
}
\end{axis}
\end{tikzpicture}
使用此数据集
\begin{filecontents*}{tabel_3.dat}
X[$m$] B[$10^{-3}T$]
1 177.12
1.5 118.30
2 88.66
2.5 70.76
3 59.03
3.5 50.73
4 44.31
4.5 39.45
5 35.42
5.5 32.25
6 29.56
6.4 27.31
7 25.30
7.5 23.64
8 22.16
\end{filecontents*}
我制作了一个拟合函数,拟合函数显示为 y=−16.32·x+ 129.59,但我需要在小数点后有 4 个数字。
答案1
您需要设置number format
:
\begin{filecontents*}{tabel_3.dat}
X[$m$] B[$10^{-3}T$]
1 177.12
1.5 118.30
2 88.66
2.5 70.76
3 59.03
3.5 50.73
4 44.31
4.5 39.45
5 35.42
5.5 32.25
6 29.56
6.4 27.31
7 25.30
7.5 23.64
8 22.16
\end{filecontents*}
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
/pgf/number format/precision=4, % <----
width=10cm,
legend style={font=\footnotesize},
grid,
}
\begin{axis}[
enlargelimits=false,
ymax=130,
ymin=0,
xmax=9,
xmin=0,
]
\addplot+[
only marks,
]
table{tabel_3.dat};
\addlegendentry{Punctele masurate};
\addplot [blue]
gnuplot [raw gnuplot] {
f(x)=a*x+b;
a=0.0001;
b=0.0001;
fit f(x) 'tabel_3.dat' using 1:2 via a,b;
plot [x=0:12] f(x);
set print "parameters.dat";
print a, b;
};
\addlegendentry{\pgfplotstableread{parameters.dat}\parameters
\pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval}
\pgfplotstablegetelem{0}{1}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
$y=\pgfmathprintnumber{\paramA} \cdot x \pgfmathprintnumber[print sign]{\paramB} $
}
\end{axis}
\end{tikzpicture}
\end{document}
我不确定这些额外的数字意味着什么,因为您的数据只准确到第二位数字。