CSV 파일 및 추세선의 데이터가 포함된 산점도

CSV 파일 및 추세선의 데이터가 포함된 산점도

나는 다음의 코드로 산점도를 만들었습니다.이 이전 질문.

\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}

관련 정보