
我用以下程式碼創建了一個散點圖之前提出的問題。
\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
\usepackage[margin=2.5cm]{geometry} %layout
\usepackage{pgfplots}
\begin{filecontents}{table3.csv}
column 1 column 2
1966 37.51817228
1960 40.56693583
1961 40.71972964
1962 40.97560208
1964 41.11687187
1963 41.25082828
1965 46.02625404
1960 46.22815872
1967 46.67800113
1961 48.39523271
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
enlarge y limits=true,
%xmin=0, xmax=2150,
%ymin=0, ymax=600,
width=15cm, height=8cm, % size of the image
grid = major,
grid style={dashed, gray!30},
ylabel=steps,
xlabel=$n$,
legend style={at={(0.1,-0.1)}, anchor=north}
]
\addplot[scatter,only marks] table [x=column 1, y=column 2, col sep=comma] {table3.csv};
%the code below is added via @Peter's comment.
\addplot[only marks] table [col sep = comma,y={create col/linear regression={y=column 2}}]{table3.csv};
\end{axis}
\end{tikzpicture}
\end{document}
散點圖最終效果很好,但我希望能夠添加趨勢線。我見過的所有趨勢線範例都是使用直接輸入到 .TeX 檔案而不是來自 .csv 檔案的資料來計算的。
是否有可能做到這一點?
我的另一個想法是繼續使用 Excel,計算趨勢線,然後將線重疊到圖表上。儘管我的文件有很多圖表,但我真的寧願能夠以更直接的方式做到這一點。
編輯:Jake 給了我很好的指導,告訴我如何使用直接輸入到 TeX 檔案的資料來執行此操作,但我在直接從 .csv 檔案解析時遇到了麻煩。我已添加到我的程式碼中並發布了我在控制台中收到的錯誤訊息。
添加代碼後,我收到錯誤訊息。
./linearreg.tex:30: Package PGF Math Error: Could not parse input '' as a float
ing point number, sorry. The unreadable part was near ''..
我的文件中的第 30 行是新增了線性迴歸方程式的行。
最終編輯:我想到了。導致此錯誤的原因是我的文件中有包含空白資料的欄位。我必須刪除這個空白資料才能計算線性迴歸線。
這是我的程式碼的最終版本:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep = comma]{table4.csv}\loadedtable
\begin{tikzpicture}
\begin{axis}[
xlabel=Weight (kg), % label x axis
ylabel=Height (cm), % label y axis
axis lines=left, %set the position of the axes
clip=false
]
\addplot[scatter, only marks] table [x=column 1, y=column 2, col sep=comma] {\loadedtable};
\addplot[very thick, red] table [col sep = comma,y={create col/linear regression={y=column 2}}]{\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
若要從資料檔案中取得資料的線性迴歸線,請使用
\addplot [no markers] table [y={create col/linear regression={y=<column name>}}] {<file name>};
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{table.dat}
x y
0 1
100 3
150 2
200 5
300 6
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
enlarge y limits=true,
width=15cm, height=8cm, % size of the image
grid = major,
grid style={dashed, gray!30},
ylabel=steps,
xlabel=$n$,
legend style={at={(0.1,-0.1)}, anchor=north}
]
\addplot[only marks] table {table.dat};
\addplot [no markers, thick, red] table [y={create col/linear regression={y=y}}] {table.dat};
\end{axis}
\end{tikzpicture}
\end{document}