我正在尝试为该图添加趋势线

我正在尝试为该图添加趋势线

我有这张图表:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b
1.230448921,0.460822919
1.342422681,0.694747354
1.431363764,0.828862164
1.505149978,0.993514561
1.568201724,1.257457266
1.62324929,1.521115401
1.672097858,1.822516751
1.716003344,2.125021632
1.755874856,2.355223203
1.792391689,2.567059417
1.826074803,2.763380773
1.857332496,2.932403886
1.886490725,3.081848588
1.913813852,3.208627804
1.939519253,3.324555792
\end{filecontents*}

\begin{filecontents*}{test.csv}
a,b
1.230448921,0.460822919
1.230448921,0.694747354
1.342422681,0.694747354
1.342422681,0.828862164
1.431363764,0.828862164
1.431363764,0.993514561
1.505149978,0.993514561
1.505149978,1.257457266
1.568201724,1.257457266
1.568201724,1.521115401
1.62324929,1.521115401
1.62324929,1.822516751
1.672097858,1.822516751
1.672097858,2.125021632
1.716003344,2.125021632
1.716003344,2.355223203
1.755874856,2.355223203
1.755874856,2.567059417
1.792391689,2.567059417
1.792391689,2.763380773
1.826074803,2.763380773
1.826074803,2.932403886
1.857332496,2.932403886
1.857332496,3.081848588
1.886490725,3.081848588
1.886490725,3.208627804
1.913813852,3.208627804
1.913813852,3.324555792
1.939519253,3.324555792
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
    title = log-log,
    xlabel={$x$},
    ylabel={$y$},
]
\addplot[blue] table [x=a,y=b,col sep=comma] {test.csv};
\end{loglogaxis}
\end{tikzpicture}
\end{document}

得到以下图表:

在此处输入图片描述

我想从中的数据添加一条红色趋势线data.csv并显示方程式。我是乳胶新手。在中test.csv,我只是修改了坐标以获得步骤;有没有更优雅的方式将点绘制为一系列步骤?非常感谢您抽出时间!

答案1

加载pgfplotstable(也会加载pgfplots)并读取test.csv,例如\datatable。然后您可以添加

\addplot[red] table[
    y={create col/linear regression={y=b}}
]{\datatable};

绘制趋势线。趋势线的斜率和截距分别存储在和中\pgfplotstableregressiona\pgfplotstableregressionb您可以为趋势线添加图例,形成如下方程

\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}

代码:

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{width=10cm,compat=1.9}
\usepackage{filecontents}
\begin{filecontents*}{test.csv}
a,b
1.230448921,0.460822919
1.230448921,0.694747354
1.342422681,0.694747354
1.342422681,0.828862164
1.431363764,0.828862164
1.431363764,0.993514561
1.505149978,0.993514561
1.505149978,1.257457266
1.568201724,1.257457266
1.568201724,1.521115401
1.62324929,1.521115401
1.62324929,1.822516751
1.672097858,1.822516751
1.672097858,2.125021632
1.716003344,2.125021632
1.716003344,2.355223203
1.755874856,2.355223203
1.755874856,2.567059417
1.792391689,2.567059417
1.792391689,2.763380773
1.826074803,2.763380773
1.826074803,2.932403886
1.857332496,2.932403886
1.857332496,3.081848588
1.886490725,3.081848588
1.886490725,3.208627804
1.913813852,3.208627804
1.913813852,3.324555792
1.939519253,3.324555792
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=comma]{test.csv}\datatable
\begin{tikzpicture}
\begin{loglogaxis}[
    title = log-log,
    xlabel={$x$},
    ylabel={$y$},
]
\addplot[blue] table [x=a,y=b,col sep=comma] {\datatable};
\addplot[red] table[
    y={create col/linear regression={y=b}}
]
{\datatable};
\addlegendentry{Data}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{loglogaxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容