How to sprintf() last data value in Gnuplot key?

Viewed 300

I'm working on a temperature graph and would like to put the last data point in the title. I can use column(2) to kind of do this but I'd like to add some descriptive text as well. I'm trying the code below to concatentate some text with the data value but getting this error: line 0: f_sprintf: attempt to print numeric value with string format

plot "/tmp/data.txt" using 1:2 with lines ls 2 title sprintf('Current :%sF', column(2))

I've tried changing the sprintf modifer to %d along with various flavors of concatenation with the dot character and haven't found the right combination.

2 Answers

Most probably there are various solutions. The first possibility which comes to my mind (I guess requiring gnuplot >5.2) is using keyentry, check help keyentry. While plotting you are asigning column 2 to a variable. After plotting, this variable holds the last value of column 2, which you use later in keyentry, which is a keyentry without plotting anything. There would also be workarounds for older gnuplot versions.

Code:

### last value into key
reset session

$Data <<EOD
1 7.1
2 6.2
3 5.3
4 4.4
5 3.5
6 2.6
7 1.7
8 0.8
EOD

plot $Data u 1:(a=$2) w lp pt 7 lc 1 notitle, \
     keyentry w lp pt 7 lc 1 ti sprintf("Last y value: %g",a)

### end of code

Result:

enter image description here

The problem here is that the title string is evaluated by gnuplot before the data is parsed and plot is performed.

A trick is to store the last value of temperature, and plot it afterwards.

T=0
plot "/tmp/data.txt" using 1:(T=column(2)) w l ls 2 notitle, \
     1/0 w l ls 2 title sprintf('Current: %.1fF', T)
Related