Christoph Grimmer
Christoph Grimmer

Reputation: 4313

gnuplot: plot multiple lines from single file and make title at end from column and key title manually

I'm quite new with gnuplot and so maybe my question has an obvious answer. Please excuse if this is too noobish.

I have the following data

20 500 1.0
30 500 0.95
40 500 0.85
50 500 0.7
60 500 0.5

20 1000 1.1
30 1000 1.05
40 1000 0.95
50 1000 0.8
60 1000 0.6

20 1500 1.2
30 1500 1.15
40 1500 1.05
50 1500 0.9
60 1500 0.7

20 2000 1.26
30 2000 1.22
40 2000 1.13
50 2000 0.99
60 2000 0.79

20 2500 1.33
30 2500 1.29
40 2500 1.21
50 2500 1.06
60 2500 0.88

Plotting this as a surface worked fine. Now I would like to plot this as 5 separate lines (using 1:3) and have the 2nd column as 'title at end' for each of the lines.

I tried

plot "demo.dat" using 1:3:2 with lines title columnhead(2) at end

but this will only label the last line (which is bogus) with 500 and ignore all the others. Also it sets 500 as title in the key box (which I would like to set to another string). Is that possible or do I have to split the blocks into several files as suggested in How to plot single/multiple lines depending on values in a column with GNUPlot ?

Upvotes: 4

Views: 5552

Answers (2)

theozh
theozh

Reputation: 25734

Another rather general solution. No need for different data format or for splitting the data into several files. Of course, it would be easier if the data was split into subblocks by two empty lines, however, the OP's data is separated only by single empty lines. How to handle this without modifying the data outside gnuplot?

For the following solution you don't have to know in advance how many subblocks your data has and how many datapoints there are in one subblock.

Furthermore:

  • different colors for the subblocks
  • value of column 2 as label at the end
  • some other text can be placed in the legend/key

Edit:

  • each subblock can have different number of datapoints
  • works with gnuplot>=4.6.0

Data: SO32603146.dat

10 500 1.05
20 500 1.0
30 500 0.95
40 500 0.85
50 500 0.7
60 500 0.5

20 1000 1.1
30 1000 1.05
40 1000 0.95
50 1000 0.8

20 1500 1.2
30 1500 1.15
40 1500 1.05
50 1500 0.9
60 1500 0.7
70 1500 0.51
80 1500 0.30

20 2000 1.26
30 2000 1.22
40 2000 1.13
50 2000 0.99
60 2000 0.79
70 2000 0.61

20 2500 1.33
30 2500 1.29
40 2500 1.21
50 2500 1.06
60 2500 0.88

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

### plotting some subblock data with label at the end
reset

FILE = "SO32603146.dat"

set rmargin 7
set key at graph 1.0,0.95 noautotitle

addPosLabel(colX,colY,colL) = (x0=x1,x1=column(colX), y0=y1,y1=column(colY), \
    L0=L1,L1=strcol(colL), b0=b1,b1=column(-1),b0!=b1 ? \
    myLabels = myLabels.sprintf(" %g %g %s",x0,y0,L0) : 0, column(colY))

x1 = y1 = b1 = NaN
L1 = myLabels = ''
PosX(i)  = real(word(myLabels,int(i*3+3)))
PosY(i)  = real(word(myLabels,int(i*3+4)))
Label(i) = word(myLabels,int(i*3+5))

plot FILE u 1:(addPosLabel(1,3,2)):-1 w lp pt 7 lc var, \
     myLabels=myLabels.sprintf(" %g %g %s",x1,y1,L1), \
     '+' u (x0=int($0*3+5),PosX($0)):(PosY($0)):(word(myLabels,x0)) \
         every ::::(words(myLabels)-2)/3-1 w labels left offset 1,0 ti "Some other text\n for the legend"
### end of script

Result:

enter image description here

Upvotes: 1

Sébastien Guarnay
Sébastien Guarnay

Reputation: 126

You may try in two steps:

 plot 'demo.dat' u 1:3 notitle with lines, \
  'demo.dat' u 1:3:2 every ::4::4 notitle with labels
  • first plotting the data.

  • then adding a label at the last point of each block (composed of 5 points going from 0 to 4), or at the point before the last one replacing ::4::4 by ::3::3.

Upvotes: 1

Related Questions