3

I would like to plot a graph from data created inside pgfplottable.

I have one file of data data.dat:

x,y
1,2
2,3
3,4
4,5
5,8
6,9
7,34

And this code :

\documentclass{beamer}


\usepackage{etex}
\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{epstopdf}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{pstricks}
\usepackage{array}

\usepackage{graphicx}

\usepackage{filecontents}

\begin{document}

\begin{frame}


\pgfplotstableread[col sep = comma]{data.dat}\loadedtable
\pgfplotstableset{
create on use/new/.style={
create col/expr={\thisrow{x}^(2)}},
create on use/new2/.style={
create col/expr ={\thisrow{y}/((7.75))}
}
}

\end{frame}

\end{document}

So, I have, from my data, created two other columns of data.

Now, I would like to plot new in relation to new2 and make a fit of this plot.

Do someone know how to do this ?

3

You need

\begin{tikzpicture}
\begin{axis}
\addplot table [x=new,y=new2] {\loadedtable};
\addplot table[x=new,y={create col/linear regression={y=new2}}] {\loadedtable};
\end{axis}
\end{tikzpicture}

enter image description here

The complete example:

\documentclass{beamer}

\usepackage{etex}
\usepackage[frenchb]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{epstopdf}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{array}
\pgfplotsset{compat=newest}

\usepackage{graphicx}

\usepackage{filecontents}
\begin{filecontents*}{data.dat}
x,y
1,2
2,3
3,4
4,5
5,8
6,9
7,34
\end{filecontents*}

\begin{document}

\begin{frame}


\pgfplotstableread[col sep = comma]{data.dat}\loadedtable
\pgfplotstableset{
create on use/new/.style={
create col/expr={\thisrow{x}^(2)}},
create on use/new2/.style={
create col/expr ={\thisrow{y}/((7.75))}
  }
}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=new,y=new2] {\loadedtable};
\addplot table[x=new,y={create col/linear regression={y=new2}}] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}
2
  • Thanks a lot, but for the fit to be the fit of [new,new2], we need to write \addplot table[x=new,y={create col/linear regression={y=new2}}] {\loadedtable}; Now it's completely solved.
    – zeslayer
    Jul 28 '15 at 19:09
  • @zeslayer True. The documentation suggests that pgfplots would pick the right x column from the previous plot, but evidently this was not the case. I updated the answer. Jul 28 '15 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.