48

I have data in some text file which has let's say 10000 rows and 2 columns. I know that I can plot it easily by plot "filename.txt" using 1:2 with lines . What I want is however just plotting let's say the rows from 1000 to 2000 or any other reasonable selection. Is it possible to do that easily? Thank you very much in advance.

jkt
  • 2,538
  • 3
  • 26
  • 28
  • maybe this can help you too: http://stackoverflow.com/questions/6564561/gnuplot-conditional-plotting-plot-col-acol-b-if-col-c-x – Thiago Neves Nov 07 '16 at 23:27

5 Answers5

100

It appears that the "every" command in gnuplot is what you're looking for:

plot "filename.txt" every ::1000::2000 using 1:2 with lines

Alternatively, pre-process your file to select the rows in which you are interested. For example, using awk:

awk "NR>=1000 && NR<=2000" filename.txt > processed.txt

Then use the resulting "processed.txt" in your existing gnuplot command/script.

Stuart Lange
  • 4,049
  • 6
  • 24
  • 30
  • If you are on linux or mac osx, you should have awk already (it is part of the standard *nix toolset, like sed and grep). I would start by running the above command as a pre-processing step (before launching gnuplot), which will create a new data file "processed.txt" while leaving your existing file unharmed. You should then use "processed.txt" in place of "filename.txt" in your plotting commands. The manual for awk can be found here: http://www.gnu.org/software/gawk/manual/gawk.html – Stuart Lange Mar 02 '12 at 16:44
36

Simpler:

plot "<(sed -n '1000,2000p' filename.txt)" using 1:2 with lines
Community
  • 1
  • 1
kev
  • 155,172
  • 47
  • 273
  • 272
9

You can probably cut out the reliance on an external utility (If your system doesn't have them installed for example) using the pseudo-column 0.

see help plot datafile using pseudocolumn

Try something like:

LINEMIN=1000
LINEMAX=2000

#create a function that accepts linenumber as first arg
#an returns second arg if linenumber in the given range.
InRange(x,y)=((x>=LINEMIN) ? ((x<=LINEMAX) ? y:1/0) : 1/0)

plot "filename.txt" using (InRange($0,$1)):2 with lines

(tested on Gnuplot 4.4.2, Linux)

mgilson
  • 300,191
  • 65
  • 633
  • 696
1

Gnuplot ignores NaN values. This works for me for a specified range of the x coordinate. Not sure how to specify row range though.

cutoff(c1,c2,xmin,xmax) = (c1>=xmin)*(c1<=xmax) ? c2 : NaN
plot "data.txt" u 1:(cutoff(($1),($2),1000,2000))
1

I would recommend some commandline tools like sed, grep or bash. In your example

head -n 2000 ./file.data > temp.data

and

tail -n 1000 temp.data > temp2.data

might work. But haven't tested if such large numbers work with head and tail.

Nick Roz
  • 3,918
  • 2
  • 36
  • 57
BREMI
  • 794
  • 1
  • 7
  • 12
  • This combined with the first answer helped me: plot "<(tail -n 90 filename.txt)" ... to read most recent 90 lines of a dataset. – JGC Aug 28 '20 at 23:52