Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a data file containing a sequence of numbers, to be plotted against integers starting from 1. So the x co-ordinate is the row number of the data in the file. How to plot this data using pgfplots?

UPDATE1: my data file looks like this

%cmc curve of nist14G21600V0P8T1sumFuse.bin

73.037

75.2963

76.7407

77.8889

79.1111

79.8519

80.3333

80.7407

81.0741

81.4444

UPDATE 2:

When the above data is plotted using the command

\addplot  table [x expr=\coordindex + 1,y index=0]{datafile.txt};

the x axis does not start with 1. It starts with 0. Any suggestions?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Based on the new request of @suresh:

pgfplots can read a data file and plot the contents. To use a special column of the data file you can use y index for the y values. The first column has the index 0. For the x axis you can use \coordindex started by 0. To start at 1 you can use \coordindex+1.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{datafile.dat}
%cmc curve of nist14G21600V0P8T1sumFuse.bin

73.037

75.2963

76.7407

77.8889

79.1111

79.8519

80.3333

80.7407

81.0741

81.4444
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot +[only marks,] table[x expr=\coordindex+1,y index=0] {datafile.dat};
\end{axis}
\end{tikzpicture}
\end{document}

ORIGINAL

You can use the option x expr=\coordindex.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{datafile.dat}
Y-Value
2
5
8
1
5
7
6
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot +[only marks] table[x expr=\coordindex,y=Y-Value] {datafile.dat};
\end{axis}
\end{tikzpicture}
\end{document}

share|improve this answer
    
thanks for the reply. But I face some other issues now. First of all my file does not have a column name. What it has is a bunch of lines starting with % character followed by data line by line. Basically I was using gnuplot so far and the file is generated with gnuplot in mind. I tried giving just table[x expr = \coordindex]{datafilename} but it gave me an error message saying that Sorry, the requested column number '1' in table 'datafilename' does not exist!? So what to do now? –  suresh Mar 12 '12 at 20:37
    
@suresh: Nice information. Please add this information with a small example file to your question. –  Marco Daniel Mar 12 '12 at 20:44
    
I have just given the sample datafile. I tried adding header=false, but didn't help either. –  suresh Mar 12 '12 at 20:49
    
@suresh: I edited my answer. –  Marco Daniel Mar 12 '12 at 20:57
    
Thanks it helped, but two more questions. I get an error message saying that ! Package pgfkeys Error: I do not know the key '/tikz/skip first n' and I am going to ignore it. Perhaps you misspelled it. I checked the log file and found out that the versions are: Package: pgf 2008/01/15 v2.10 (rcs-revision 1.12),pgfplots 2010/07/14 Version 1.4.1. So are you using a newer version? My second question is: cant pgfplots by default ignore lines starting with '%'. I remember to have read somewhere that lines starting with % are ignored. –  suresh Mar 12 '12 at 21:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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