24

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?

2
  • what about removing the unused data points from the text files? It's probably easier.
    – pluton
    Commented Mar 12, 2012 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
    Commented Mar 12, 2012 at 23:32

2 Answers 2

34

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

\addplot[<your options here>,each nth point={100}] ...
1
  • 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
    Commented Mar 13, 2012 at 2:18
19

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.

3
  • 2
    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
    Commented Mar 13, 2012 at 3:13
  • 1
    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
    Commented Mar 13, 2012 at 3:24
  • 2
    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
    Commented Mar 13, 2012 at 6:15

You must log in to answer this question.

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