1

I wish to plot the second, third, and fourth from the rightmost column using Gnuplot. In awk, we can use ($NF-1). But in Gnuplot, not sure how can I designate a column from the rightmost column with 'using'.

Is this possible to use awk in Gnuplot to plot 3rd from the right column vs 4th from the right column? Or is this something that we must need to use shell script?

I have a lot of long text files to plot, so I cannot create new text files to rewrite the file using awk and then use Gnuplot. That would be too time-consuming. I wish to use Gnuplot to make plots from 2nd, 3rd, and 4th from the right.

exsonic01
  • 615
  • 7
  • 27

2 Answers2

1

you can use STATS_columns for the number of columns and use it in your plot

e.g.

nf = int(STATS_columns)
plot data.dat using 1:nf-4
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Is this possible without computing the actual statistics of the file. This seems a bit a heavy operation to obtain the value of `STATS_columns` – kvantour Jan 09 '21 at 18:25
  • 1
    you can pass it as an argument to gnuplot but if you're interested in data you probably compute stats as well. – karakfa Jan 09 '21 at 19:26
  • Okay true, you are right. Stats actually provides a lot of very useful information. – kvantour Jan 09 '21 at 19:31
  • Maybe update the answer to also include how you compute stats – kvantour Jan 09 '21 at 19:31
1

No need for awk. If you do stats you could limit it to one row with every ::0::0. It should be pretty fast. Try the following complete example:

Code:

### plotting columns from right
reset session

$Data <<EOD
11  21  31  41  51  61  71
12  22  32  42  52  62  72
13  23  33  43  53  63  73
14  24  34  44  54  64  74
15  25  35  45  55  65  75
16  26  36  46  56  66  76
17  27  37  47  57  67  77

EOD

stats $Data u 0 every ::0::0 nooutput
ColMax = STATS_columns

ColFromRight(col) = column(ColMax-col+1)

plot $Data u (ColFromRight(3)):(ColFromRight(4)) w lp pt 7
### end of code

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72