Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting on Gnuplot - skipping lines

Tags:

gnuplot

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?

like image 586
TomatoLion Avatar asked Aug 02 '26 17:08

TomatoLion


1 Answers

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
like image 103
theozh Avatar answered Aug 07 '26 01:08

theozh