3

I have some problems with reading my file in Gnuplot. For example, I have a file like this:

___________________________________________
'#current' 
month followed retweeted mentioned replied 

Jan 395 29 35 28 

Feb 380 28 32 31 

'#previous' 
month followed retweeted mentioned replied 

Jan 381 30 38 32 

Feb 378 25 42 30 

Mar 374 28 46 40
______________________________________________________

I need to read only the second block, which starts with tag "#previous". How can I do it? I tried this command:

plot "data.txt" index 'previous' using 3:xticlabel(1) axes x1y1 with lines linecolor rgbcolor "red",\

but it doesn't work. Any ideas?

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
TomatoLion
  • 81
  • 2
  • 12
  • 1
    If you are working under linux you can just filter anything behind '#previous' and pipe it to gnuplot with specific config file that will take under consideration your data format. Just remember that in this case you probably want to tell gnuplot how is your date formatted. – Greg0ry May 02 '13 at 22:20
  • Yes, I'm working under Linux, but i've just started, so it's rather difficult for me. Can you write the line, that I should use for plotting? – TomatoLion May 02 '13 at 22:28
  • Have a look on this google search: http://goo.gl/6zaiO first link comes from stack overflow http://stackoverflow.com/questions/7103531/how-to-get-the-part-of-file-after-the-line-that-matches-grep-expression-first you use that and pipe to gnuplot – Greg0ry May 02 '13 at 22:36
  • Is this really the data format? (e.g. are there really quotes around the `'#previous'` and blank lines between records?). If so, that makes this a bit more challenging (It's definitely not standard gnuplot format) – mgilson May 03 '13 at 01:05
  • "but it doesn't work" -- What fails? And, what version of gnuplot are you using? – mgilson May 03 '13 at 01:06

3 Answers3

0

Check out the answer to this question

Gnuplot: Plotting several datasets with titles from one file

I think you need to add a 1 after index like so

plot "data.txt" index 1 using 3:xticlabel(1) axes x1y1 with lines linecolor rgbcolor "red"

edit: Datasets are 0-indexed so the first block is indexed 0 and the second block (previous) has an index one. The error you mention regarding the bad line indicates a problem with our data file format. Read about the data format at these links http://www.gnuplotting.org/plotting-data/ http://lowrank.net/gnuplot/datafile2-e.html

Community
  • 1
  • 1
Kristian K.
  • 1,437
  • 1
  • 10
  • 9
  • But why do You use "1" after "index"? How can i specify my "previous" tag? I've tried plot "data.txt" i 0 t "previous" using 3:xticlabel(1) axes x1y1 with lines linecolor rgbcolor red, but it says that there is bad data in 3 line.. – TomatoLion May 02 '13 at 22:50
  • Newer versions of gnuplot should accept a string for the index. – mgilson May 03 '13 at 01:06
  • @KristianK. in OP's case `index 1` will not work because OP's data does not have **two** empty lines as separator between the blocks – theozh May 23 '23 at 19:40
  • @mgilson gnuplot 4.6.0 (March 2012) accepts string for the index, but it requires a block starting with a commented line, e.g. `#previous` without quotes and as mentioned in the comment above **two** empty lines as separator. – theozh May 23 '23 at 19:42
0

Let us put everything together:

Following this link you can learn how to filter the file (so you can get everything after certain line)

So in our case:

sed -e '1,/previous/d' data.txt > gnuplot some_gnuplot_options

I write this from my windows devel machine so can't validate but this should give you some idea how can you do this.

I would also recommend defining gnuplot config file you feed to gnuplot. Just create settings.pg and put there something like this (that's my example from some work I have done for myself so it does not apply to your data format):

set terminal png size 1024, 360
set output "load.png"
set datafile separator "\t"
set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
set format x "%m/%d\n%H:%M"
set xrange["2012-04-29 11:00:00":"2012-05-01 11:58:00"] noreverse nowriteback
set autoscale y
set grid
set key left box
plot "chart.txt" using 1:2 t 'column_1' with lines, "chart.txt" using 1:3 t 'column_2' with lines

So then your gnuplot call would look something like this:

sed -e '1,/previous/d' data.txt > gnuplot your_pg_file.pg

You would also want to check time formating from gnuplot manuals here.


edit:

If that's your university homework you should not post your question here :-) I don't want to be boring or something but is it not the goal of homework that you find your solution following documentation study and trying different things eh? :-)

Community
  • 1
  • 1
Greg0ry
  • 931
  • 8
  • 25
  • I can't use any Linux comands like "sed" because after I must upload the solution to university server and the solution will be tested. It must contain only Gnuplot comands. – TomatoLion May 02 '13 at 22:53
  • 'm erasmus student and the teacher of this subject doesn't care how can we do it. He doesn't give us any documentation or something, just tells "use Google" and that's all. I'm googling for solution for 2 days, but I haven't found good one yet.. – TomatoLion May 02 '13 at 23:08
  • But we leave in `google` era :-) you can find your documentation there can't you? Well, I'm not judging you - that's your business how you do your homework. – Greg0ry May 02 '13 at 23:35
  • FWIW -- your pipe is in the wrong place. Gnuplot will never see the data since you haven't told it to read anything. It's better to inline the pipe within the plot command: `plot '< sed -e ... data.txt' using 1:2 ...` – mgilson May 03 '13 at 01:04
  • I could not test it so you might be right. I'll update it to another form I thing might work. Thanks. – Greg0ry May 03 '13 at 01:30
0

Although this question is pretty old and not yet accepted and the OP asks in the comments about a gnuplot-only solution, let me give one.

It is a bit confusing what is called a block in gnuplot. "Blocks" which can be addressed via index are separated by two (or more) empty lines. "(sub)blocks" which can be addressed via every :::b::b are separated by exactly one empty line.

A few things which are not given in your script/data need to be clarified:

  • blocks in gnuplot are separated by two empty lines. You have only one empty line
  • blocks can be addressed by names, i.e. a comment line starting with # plus <keyword>. e.g. #previous, but not '#previous'.

Furthermore:

  • if your data contains empty lines gnuplot will break the lines. In your data every other line is an empty line, hence plotting with linestyle with lines will not show anything. Whereas with linespoints will only show the points, not the lines.

A solution your your (unchanged) data:

  1. find the (sub)block which contains the keyword '#previous' using stats.
  2. plot the data starting from that (sub)block with points and again with vectors connecting the points.

Data: SO1648594.dat

___________________________________________
'#current' 
month followed retweeted mentioned replied 

Jan 395 29 35 28 

Feb 380 28 32 31 

'#previous' 
month followed retweeted mentioned replied 

Jan 381 30 38 32 

Feb 378 25 42 30 

Mar 374 28 46 40
______________________________________________________

Script: (works with gnuplot>=4.6.0, March 2012)

### plot starting from specific keyword
reset

FILE = "SO1648594.dat"

stats FILE u (strcol(1) eq  "'#previous'" ? r0=column(-1) : 0) nooutput
set key noautotitle
set offsets 0.5,0.5,0.5,0.5

plot FILE u 0:3:xtic(1) every :::r0+1 w p pt 7 lc rgb "red", \
     x1=y1=NaN '' u (x0=x1,x1=column(0)):(y0=y1,y1=$3):(x0-x1):(y0-y1) every :::r0+1 w vec nohead lc rgb "red"
### end of script

Result:

enter image description here

Actually, a better data format would be:

Data: SO1648594_2.dat

___________________________________________
#current
month followed retweeted mentioned replied 
Jan 395 29 35 28 
Feb 380 28 32 31 


#previous
month followed retweeted mentioned replied 
Jan 381 30 38 32 
Feb 378 25 42 30 
Mar 374 28 46 40
______________________________________________________

Then the script (with the same result as above) would be reduced to:

Script: (works with gnuplot>=4.6.0, March 2012)

### plot starting from specific keyword
reset

FILE = "SO1648594_2.dat"

set key noautotitle
set offsets 0.5,0.5,0.5,0.5

plot FILE u 3:xtic(1) index "previous" w lp pt 7 lc rgb "red"
### end of script
theozh
  • 22,244
  • 5
  • 28
  • 72