'Gnuplot plotting wrong lines and some strange values as well

I am using gnuplot to postprocess some calculation that I have done and I am having hard time getting gnuplot to select the right lines as it is outputting some strange values that I do not know where come from.

The first 200 points of the results start in line 3 and stop in 202 but that is not working when I use every ::3::202. Does anyone have any suggestions of what I am doing wrong?

Gnuplot image:

enter image description here

Datafile

set terminal pngcairo transparent nocrop enhanced size 3200,2400 font "arial,40" 

set output "Mast41_voltage_muffe.png"
set key right
set samples 500, 500

set xzeroaxis ls 1 lt 8 lw 3

set style line 12 lc rgb '#808080' lt 0 lw 1
set style line 13 lt 0 lw 3
set grid back ls 12
set decimalsign '.'
set datafile separator whitespace

set ylabel "Spenna [pu]"
set xlabel "Timi [s]"

plot "mrunout_01.out" every ::3::202 using 2:3 title '5 ohm' with lines lw 3 linecolor rgb '#D0006E',\
     "mrunout_01.out" every ::203::402 using 2:3 title '10 ohm' with lines lw 3 linecolor rgb '#015DD4',\
     "mrunout_01.out" every ::403::602 using 2:3 title '15 ohm' with lines lw 3 linecolor rgb '#F80419',\
     "mrunout_01.out" every ::603::802 using 2:3 title '20 ohm' with lines lw 3 linecolor rgb '#07826A'

unset output
unset zeroaxis
unset terminal


Solution 1:[1]

every refers to the actual plottable points. In your case, you have to skip 2 lines and the bunch of data at the end of your datafile.

Since you know the actual lines you need to plot I would pre-parse the file with some external tools like sed

So you can omit the every and your plot line becomes:

plot "< sed -n '3,202p' mrunout_01.out" using 2:3 title '5 ohm' with lp lw 3 linecolor rgb '#D0006E'

With yor datafile as it is, gnuplot has problems reading it. It can't even run stats on it:

stats 'mrunout_01.out'
     bad data on line 1 of file mrunout_01.out

Solution 2:[2]

There is no need for using external tools, you can simply do it with gnuplot.

It's advantageous with your data that it is regular, every 200 points plotted in a different color. And the data you want to plot is separated by one empty line from some additional data at the end of the file which you don't want to plot. So, you simply address the 4th set of 200 lines in the 0th block via every ::600:0:799:0.

From help every:

Syntax:

  plot 'file' every {<point_incr>}
                      {:{<block_incr>}
                        {:{<start_point>}
                          {:{<start_block>}
                            {:{<end_point>}
                              {:<end_block>}}}}}

Comments:

  • you can skip two lines at the beginning of the files with skip 2
  • you can plot your curves in a loop plot for [i=1:4] ...
  • you can define your color myColor(n) via index n from a string "#D0006E #015DD4 #F80419 #07826A"
  • you can define the legend myTitle(n) also from a list "5 10 15 20"

Script: (tested with gnuplot 5.0.0, version at the time of OP's question)

### plot parts of a file in a loop
reset session

FILE = "SO36103041.dat"

myColor(n)   = word("#D0006E #015DD4 #F80419 #07826A",n)
myTitle(n)   = word("5 10 15 20",n)

set xlabel "Timi [s]"
set ylabel "Spenna [pu]"
set yrange[0:30]

plot for [i=1:4] FILE u 2:3 skip 2 every ::((i-1)*200):0:(200*i-1):0 \
                      w l lw 3 lc rgb myColor(i) ti myTitle(i)
### end of script

Result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 theozh