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 datafile containing thousands of points of which I need to plot only the first 100 points. How to do this using pgfplots? I tried the following with no effect.

\begin{axis}[
xlabel=k,
ylabel=$R_k$,
xmin=1,
samples y = 100]

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

I also tried,

\begin{axis}[
xlabel=k,
ylabel=$R_k$,
xmin=1
]

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

Any suggestions?

share|improve this question
    
what about removing the unused data points from the text files? It's probably easier. –  pluton Mar 12 '12 at 23:22
    
thanks. That is what I am doing now. But I want to plot a graph and and an inset graph with smaller number of data points. I want to do this for a series of examples and felt that if a pgfplots option is available (a newbie here), would prefer that instead of creating large number of files. –  suresh Mar 12 '12 at 23:32
add comment

2 Answers

When using addplot, there is an option to select every nth point:

\addplot[<your options here>,each nth point={100}] ...
share|improve this answer
    
Thank you for exposing me to this option. What I wanted was a method by which I can select the first 100 points only. I believe I can use skip coords between index={begin}{end} option except that I have to know the end value. BTW is there any keyword denoting the end value of index? –  suresh Mar 13 '12 at 2:18
add comment

With the indirect help of Mark S. Everitt, I figured out how to do this.

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

From the pgfplots manual, `/pgfplots/skip coords between index={begin}{end}

A style which appends an x filter which discards selected coordinates. The selection is done by index where indexing starts with 0, see \coordindex. Every coordinate with index begin ≤ i < end will be skipped.`

share|improve this answer
1  
Ah, I see what you wanted now. Sorry, should have read the question more carefully. This can be improved however. You can automatically acquire the total number of rows in your table with \pgfplotstablegetrowsof{datafile.txt}\pgfmathsetmacro\nrows{\pgfmathresult}. Now instead of putting in the 2700 manually you can replace it with \nrows and know that it will work for any number of rows. –  qubyte Mar 13 '12 at 3:13
    
Wow, nice. I presume that \pgfmathresult would contain the result of pgfplotstablegetrowsof which you are assigning to the variable nrows using the ``pgfmathsetmacro` command. What I did is, I gave a large number to the end value and TeX anyway didnt complain :) –  suresh Mar 13 '12 at 3:24
    
You're right. You can put this in right above the \addplot to get the desired result. The tools provided by pgfplotstable mean that you can further automate this by only loading the datafile once. –  qubyte Mar 13 '12 at 6:15
add comment

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.