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
  • 35,775
  • 16
  • 76
  • 95
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

2 Answers2

1

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,291
  • 1
  • 9
  • 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
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
  • 793
  • 6
  • 24
  • 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