Moon1shadow
Moon1shadow

Reputation: 35

How do I plot every nth datapoint in a csv that is all a single row

I am tasked with plotting some data generated by a person who apparently has an aversion to newlines/columns.

I have a file with the format like this:

i0,x0,y0,i1,x1,y2,i2,x2,y2,i3,x3,y3,...in,xn,yn

I need to plot i vs. y.

I have come across the "every" command but that seems to be for skipping rows in data with columns.

I am running on gnuplot 4.6 on Windows 7. Can this be done only in gnuplot? If not, what is the best way to insert the new line characters. I can hop on a Linux box but to do so but it does not have gnuplot available. Either way, I would prefer a self-contained plt file as this will come up again.

Upvotes: 2

Views: 1261

Answers (1)

Christoph
Christoph

Reputation: 48390

Yes, you can plot this data, but that requires a bit of tricking, since gnuplot usually reads the data row-by-row.

You must read your data with matrix, save the values of every third column, starting with the zeroth, and then use this saved value as x-value when the y-value is read in (column number modulo 3 == 2):

set datafile separator ','
x = 0
plot 'test.dat' matrix using (int($1) % 3 == 0 ? x = $3 : x = x, (int($1) % 3 == 2 ? x : 1/0)):(int($1) % 3 == 2 ? $3 : 1/0) w lp ps 2 pt 7 

With the test data

0.1,2,3,0.2,5,6,0.4,8,9,0.8,11,12,1.6,14,15,3.2,17,18

I get the plot

enter image description here

Note, that you get a line plot only with gnuplot version 5.0. With 4.6, the 1/0 causes the lines to be interrupted, and you see only points.

With previous versions you must first plot the data to an intermediate file, and then plot this again using a conventional plot command:

set datafile separator ','
x = 0
set table 'test.tab'
plot 'test.dat' matrix using (int($1) % 3 == 0 ? x = $3 : x = x, (int($1) % 3 == 2 ? x : 1/0)):(int($1) % 3 == 2 ? $3 : 1/0) w lp ps 2 pt 7 
unset table

set datafile separator white
plot 'test.tab' using 1:2 every 3::2 w lp ps 2 pt 7 

Upvotes: 2

Related Questions