
私は以下のコードを使って散布図を作成しました。前回の質問。
\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}
散布図はうまくいきましたが、トレンドラインを追加できるようにしたいと思います。私が見たトレンドラインの例はすべて、.csv ファイルからではなく、.TeX ファイルに直接入力されたデータを使用して計算されたものです。
これは可能ですか?
私が他に考えたのは、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 行目は、線形回帰方程式が追加された行です。
最終編集:分かりました。このエラーは、ファイルに空白データの列があったために発生しました。線形回帰線を計算するには、この空白データを削除する必要がありました。
これが私のコードの最終的な mwe です:
\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}