user1667906
user1667906

Reputation: 53

GnuPlot: multiple graphs by first value

I'm new to GnuPlot and find it a bit confusing. I have data like this:

sensor1 timestamp   temperature
sensor1 timestamp   temperature
sensor1 timestamp   temperature
sensor2 timestamp   temperature
sensor2 timestamp   temperature
sensor2 timestamp   temperature
sensor3 timestamp   temperature
sensor3 timestamp   temperature

I use the following for only one sensor which produces the expected result.

plot 'sensors.txt' using 2:($3/100.0):1 with lines

I want draw a graph for every sensor using the first value as the graph's title. It does not sound like a to complex problem but I could not figure it out other than splitting the data up into one file for each sensor. Is there a more elegant solution?

Upvotes: 0

Views: 130

Answers (1)

theozh
theozh

Reputation: 25734

A solution to this is filtering by keyword via the ternary operator (see help ternary).

You don't show example data, so I assume something.

Code:

### split data by keyword for each plot
reset session

$Data <<EOD
sensor1  2020-06-21 12:00  24.3
sensor1  2020-06-21 13:00  25.3
sensor1  2020-06-21 14:00  22.3
sensor2  2020-06-21 15:00  23.3
sensor2  2020-06-21 16:00  22.3
sensor2  2020-06-21 17:00  21.3
sensor3  2020-06-21 18:00  25.3
sensor3  2020-06-21 19:00  23.3
sensor3  2020-06-21 20:00  27.3
EOD

myTimeFmt = "%Y-%m-%d %H-%M"
myFilter(fcol,key,dcol) = strcol(fcol) eq key ? column(dcol) : NaN
set datafile missing NaN
set format x "%Y\n%m-%d\n%H:%M" time
set xtics font ",8"
set ytics 1

set multiplot layout 3,1
    do for [i=1:3] {
        myKey = sprintf("sensor%d",i)
        set title myKey
        plot $Data u (timecolumn(2,myTimeFmt)):(myFilter(1,myKey,4)) w lp pt 7 lc i title myKey
    }
unset multiplot
### end of code

Result:

enter image description here

Upvotes: 2

Related Questions