1

I have a data set to which I want to add a trend line. For my system, there will always be a fixed point on (2, 0). I have the line representing this system, but currently I do not account for the fixed point. Is there functionality to achieve this?

\begin{figure}
\begin{tikzpicture}
\begin{axis} [
    ylabel={Some Time [ms]},
    ]
    \addplot[only marks, color=red, error bars/.cd,
                       y dir=both, y explicit]
        table [x=num_of_nodes, y=MyData, y error=stdev_delay_var] {MyData.dat};
    \label{plot_one}
    \addplot +[mark=none, dashed] 
        table[y={create col/linear regression={y=MyData}}]
        {MyData.dat};
\end{axis}
\end{tikzpicture}
\caption{Test Results}
\label{fig:test_results}
\end{figure}
1
  • You could add a variance list with a very small variance assigned to (2,0). Commented May 9, 2016 at 21:30

1 Answer 1

1

You can do this by using the raw gnuplot function. For more details please have a look at the comments in the code.

% used PGFPlots v1.14
    % because there where no data to play with, I created some dummy data
    \begin{filecontents*}{MyData.dat}
        x    y
        0    2.831
        1    2.843
        2    4.580
        3    4.808
        4    6.825
        5    7.000
        6    8.611
        7    9.295
        8    9.159
        9    11.773
        10   11.923
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        % some options to have a nice picture
        xmin=-1,
        ymin=-1,
        axis lines=middle,
        legend style={
            at={(axis cs:9.8,0.2)},
            anchor=south east,
        },
        legend cell align=left,
    ]
        % show the data points
        \addplot+ [only marks] table [
            x=x,
            y=y,
        ] {MyData.dat};

        % here the "normal" linear regression using the equation $y = a*x + b$
        \addplot+ [
            mark=none,
            blue,
            thick,
        ] table [
            y={create col/linear regression={y=y}}
        ] {MyData.dat};

        % here the "modified" linear regression using the equation $ y = a*x + 2$
        % (for that we use the `raw gnuplot' feature ...
        \addplot+ [
            mark=none,
            red,
            thick,
        ] gnuplot [raw gnuplot,id=mod_lin_reg] {
            % ... define the function to fit, ...
            f(x)=a*x + 2;
            % ... fit the function ...
            fit f(x) 'MyData.dat' using 1:2 via a;
            % ... set the number of samples to two (which is enough for a line) ...
            set samples 2;
            % ... and plot the result in the given domain
            plot [x=0:10] f(x);
        };
            % this is used to calculate the slope of the modified function
            % (for the legend entry)
            \pgfplotstableread[header=false]{\jobname.mod_lin_reg.table}{\datatable}
            \pgfplotstablegetelem{1}{[index]1}\of{\datatable}
                \pgfmathsetmacro{\NewSlope}{(\pgfplotsretval-2)/10}

        \legend{
            data points,
            $y = \pgfmathprintnumber{\pgfplotstableregressiona} \, x
                 + \pgfmathprintnumber{\pgfplotstableregressionb}$,
            $y = \pgfmathprintnumber{\NewSlope} \, x + 2$,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .