[Tex/LaTex] Plot a linear function with pgf plot


Hi I would like to plot a graph of load voltage vs load current, I have been trying to do it with a tikzpicture, but wonder if maybe it can be done with a tikzplot? The bellow code is sort of what I am after, I want to plot the data and measure the gradient and get the yintercept, I'm quite new at using pgfplot so quite a step learning curve for me. My plot points would be


I can figure out the equation for the line and put this on as a label?


\draw [ultra thick,-latex] (0,0)--(10,0) node [right, above]{$I_L$ Load Current};
\draw [ultra thick,-latex] (0,0)--(0,12) node [right]{$V_L$Load Voltage};
\coordinate (X2) at (4,0);
\coordinate (X1) at (6,0);
\coordinate (P1) at (0,5);
\coordinate (P2) at (0,7);
\coordinate (b)  at (4,5);
\coordinate (d)  at (6,7);
\coordinate (a)  at (4,7);
\coordinate (c)  at (6,5);
\node at (X2) [below] {$X2$};
\node at (X1) [below] {$X1$};
\node at (P2) [left] {$P2$};
\node at (P1) [left] {$P1$};
\draw [dashed] (a)node [above right] {$a$}--(X2);
\draw [dashed] (d)node [above right] {$d$}--(X1);
\draw [dashed] (P1)--(c)node [above right] {$c$};
\draw  (P2)--(d)node [above right] {$d$};
\node at (b) [above right]{$b$};
%\draw (d)--++(0:2)node [right] {$ MC=AC$};
\draw (c)--(a)--++(135:5.65);
\draw (c)--++(-45:2) node [below] {$D$};


This is what I have so far, but cant get it to look like the above example.. I'd like to take the dashed lines from some of my data points

\documentclass[convert = false, border=10pt]{standalone}
% http://sourceforge.net/projects/pgfplots/
\addplot[color=blue,mark=x] coordinates {


Best Answer

This is one possible solution where the linear regression from pgfplotstable library is utilized. And the linear model is computed and shown in the legend. Y interception is also shown in the graph.

enter image description here


\documentclass[convert = false, border=10pt]{standalone}
% http://sourceforge.net/projects/pgfplots/
X     Y   
0.750   7.5
0.409   8.192
0.2812 8.437
0.2143 8.571

    legend cell align = left,
    legend pos = north east,
every axis x label/.style={at={(current axis.south east)},below=7mm, anchor=east},
% raw datum read from an input file called data.csv
\addplot[color=blue,mark=x] table[row sep=\\,x=X,y=Y]{data.csv};
\addlegendentry{Linear function}
% -- linear regression
\addplot+[no markers,red] table[row sep=\\,
 y={create col/linear regression={y=Y}}]{data.csv};
\addlegendentry{linear trend $\left(y=\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}\right)$}
% -- extrapolation to find intersections.
\addplot+[no markers,red,shorten >=-2cm] table[row sep=\\,
 y={create col/linear regression={y=Y}}]{data.csv};
% -- labelling
\draw[dashed] (axis cs: 0,8.571)node[xshift=0.3cm,pin={left:$P_1$}]{}--(axis cs: 0.2143,8.571);
\draw[dashed] (axis cs:0,8.192)node[xshift=0.3cm,,pin={left:$P_2$}]{}--(axis cs: 0.409,8.192) |- 
(axis cs:0,7)node[pos=0.5,pin={below:$X_2$},inner sep=0pt]{};
\draw[dashed] (axis cs: 0,8.571)-|(axis cs: 0.409,8.192);
\draw[dashed] (axis cs: 0.2143,8.571) |- (axis cs:0,7)node[pos=0.5,pin={below:$X_1$},inner sep=0pt] {};