Home > Software engineering >  Gnuplot three .csv-files with different times in same timefmt
Gnuplot three .csv-files with different times in same timefmt

Time:11-28

I have 3 .csv-files in same timefmt, but with different times and i want to plot them over each other.

The files are build like this:

NO.;Time;ms;" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";" V";"A1234567890";"A1234567890";"AO1234/WLAO12345"
1;2022-11-25 15:43:42;0; 3.344;-0.013; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001;-0.001; 0.000; 0.002; 0.001; 0.000; 0.000;-0.001; 0.000;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
2;2022-11-25 15:43:42;200; 3.344;-0.012; 0.002; 0.001; 0.001; 0.001;-0.001; 0.001;-0.001; 0.000; 0.002; 0.001; 0.000; 0.000;-0.001; 0.000;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
3;2022-11-25 15:43:42;400; 3.345;-0.013; 0.003; 0.001; 0.000; 0.001;-0.001; 0.001;-0.001; 0.000; 0.001; 0.001; 0.001; 0.000;-0.001; 0.001;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
4;2022-11-25 15:43:42;600; 3.344;-0.012; 0.002; 0.001; 0.000; 0.001;-0.001; 0.001; 0.000; 0.000; 0.002; 0.001; 0.001; 0.001;-0.001; 0.001;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
5;2022-11-25 15:43:42;800; 3.344;-0.012; 0.002; 0.000; 0.000; 0.001;-0.001; 0.001;-0.001; 0.000; 0.002; 0.001; 0.000; 0.001;-0.001; 0.001;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
6;2022-11-25 15:43:43;0; 3.344;-0.012; 0.002; 0.000; 0.000; 0.001;-0.001; 0.001; 0.000; 0.000; 0.001; 0.000;-0.001; 0.000;-0.001; 0.001;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
7;2022-11-25 15:43:43;200; 3.344;-0.012; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001;-0.001;-0.001; 0.001; 0.001;-0.001;-0.001;-0.002; 0.001; 0.000;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
8;2022-11-25 15:43:43;400; 3.344;-0.012; 0.002; 0.000;-0.001; 0.001;-0.002; 0.001;-0.001;-0.001; 0.001; 0.001; 0.000;-0.001;-0.001; 0.000; 0.000; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
9;2022-11-25 15:43:43;600; 3.345;-0.012; 0.002; 0.000;-0.001; 0.000;-0.002; 0.001; 0.000;-0.001; 0.001; 0.000;-0.001;-0.001;-0.001; 0.000;-0.001;-0.001;-0.001;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
10;2022-11-25 15:43:43;800; 3.344;-0.012; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001; 0.000; 0.000; 0.001; 0.000; 0.000;-0.001;-0.001; 0.000;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
11;2022-11-25 15:43:44;0; 3.344;-0.011; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001;-0.001; 0.000; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
12;2022-11-25 15:43:44;200; 3.344;-0.011; 0.002; 0.000;-0.001; 0.000;-0.002; 0.001;-0.001; 0.000; 0.002; 0.001;-0.001;-0.001;-0.002; 0.000;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
13;2022-11-25 15:43:44;400; 3.344;-0.011; 0.002; 0.000;-0.001; 0.000;-0.001; 0.001;-0.001; 0.000; 0.001; 0.001; 0.000; 0.000;-0.002; 0.000;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
14;2022-11-25 15:43:44;600; 2.890;-0.011; 0.002; 0.000;-0.001; 0.000;-0.001; 0.001; 0.000;-0.001; 0.002; 0.001; 0.001; 0.001;-0.001; 0.001;-0.001; 0.000; 0.001;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
15;2022-11-25 15:43:44;800; 2.878;-0.012; 0.002; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000; 0.000; 0.002; 0.000; 0.000; 0.001;-0.001; 0.001;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
16;2022-11-25 15:43:45;0; 2.881;-0.013; 0.003; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.001; 0.000; 0.000; 0.000;-0.001; 0.001;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
17;2022-11-25 15:43:45;200; 2.870;-0.013; 0.002; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.000; 0.001;-0.001; 0.001; 0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
18;2022-11-25 15:43:45;400; 2.869;-0.012; 0.002; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.000; 0.000;-0.002; 0.001; 0.001; 0.000; 0.001;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
19;2022-11-25 15:43:45;600; 2.874;-0.012; 0.003; 0.000;-0.001; 0.001;-0.001; 0.001; 0.001;-0.001; 0.002;-0.001; 0.000; 0.000;-0.002; 0.001;-0.001;-0.001; 0.000;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
20;2022-11-25 15:43:45;800; 2.869;-0.011; 0.003; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000; 0.000; 0.001;-0.001; 0.000; 0.000;-0.002; 0.001;-0.001;-0.001; 0.001;-0.002;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
21;2022-11-25 15:43:46;0; 2.869;-0.010; 0.002; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.000; 0.000;-0.001; 0.001; 0.000;-0.001; 0.001;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
22;2022-11-25 15:43:46;200; 2.870;-0.010; 0.002; 0.001;-0.001; 0.001;-0.001; 0.001; 0.001;-0.001; 0.003; 0.001; 0.000; 0.001;-0.002; 0.001;-0.001;-0.001; 0.001;-0.002;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
23;2022-11-25 15:43:46;400; 2.873;-0.011; 0.003; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.001; 0.001; 0.000; 0.000;-0.002; 0.001;-0.001;-0.001; 0.001;-0.002;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
24;2022-11-25 15:43:46;600; 2.868;-0.011; 0.002; 0.001;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.001; 0.001;-0.002; 0.001;-0.001;-0.001; 0.001;-0.002;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
25;2022-11-25 15:43:46;800; 2.870;-0.010; 0.002; 0.000;-0.001; 0.001;-0.001; 0.001; 0.000;-0.001; 0.002; 0.000; 0.000; 0.001;-0.001; 0.001;-0.001;-0.001; 0.001;-0.002;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
...

Here I just need the 2nd & 4th column.

And my Code look like:

set grid
set datafile separator ";"
set title 'xxx'
set title font ",12"
set ylabel 'U/V' font ",12"
set key box font ",12"
#myformat = "%Y-%m-%d %H:%M:%S"
#set key at strptime(myformat,"2022-11-24 18:02:55"), 3.005
set xtics time
set xlabel 'time' font ",12"
set yrange [2.4:3.6]
set ytics font ",10"
set y2tics font ",10"
set border 11
set border lw 2
set xtics font ",8"
set tics nomirror
set term wxt size 1200, 460
set xdata time
#set timefmt "%s"
#set timefmt "%Y-%m-%d %H:%M:%s"
#set timefmt "%tH:%tM:%tS"
#set format x "%M:%S" time
#set xrange ["2022-11-25 15:40:00":"2022-11-26 17:05:00"]
#set xrange ["17:20:00":"18:47:00"]
#set xrange 0:7000
#print strptime("%Y-%m-%d %H:%M:%S",s)
#myTimeFmt = "%Y-%m-%d %H:%M:%S"
#Normalize(c) = (t==0?(t0=timecolumn(c,myTimeFmt),t=1):NaN, timecolumn(c,myTimeFmt)-t0)

#plot t=0 'xxx.CSV' using (Normalize(2)):4 every ::43::6849 title "aaa" lt 7 lc 7 with lines, \
#'yyy.CSV' using (Normalize(2)):4 every ::43::6382 title "bbb" lt 7 lc 7 with lines, \
#'zzz.CSV' using (Normalize(2)):4 every ::43::5928 title "ccc" lt 7 lc 7 with lines

plot 'xxx.CSV' using (timecolumn(2, "%Y-%m-%d %H:%M:%S")):4 every ::43::6849 title "aaa" lt 7 lc 7 with lines, \
'yyy.CSV' using (timecolumn(2, "%Y-%m-%d %H:%M:%S")):4 every ::43::6382 title "bbb" lt 7 lc 7 with lines, \
'zzz.CSV' using (timecolumn(2, "%Y-%m-%d %H:%M:%S")):4 every ::43::5928 title "ccc" lt 7 lc 7 with lines

I tried many different things, but without the desirable effect. Does somebody know, what I can do?

I expanded the .csv-file. And this is my actual code:

set grid
set title 'Entladung der Batterie nach verschiedenen Ladungen'
set tics nomirror
set title font ",12"
set ylabel 'U/V' font ",12"
set key box font ",12"
set xtics time
set xlabel 'time' font ",12"
set border 11
set border lw 2
set xtics font ",8"
set term wxt size 1200, 460
myFmt = "%Y-%m-%d %H:%M:%S"
set datafile separator ";"
set format x "%tH:%M:%S" #timedate
set yrange[2.5:3.6]

plot 'Entladen1C.CSV' u (t=timecolumn(2,myFmt), $0==0?t01=t:0, t-t01):4 every ::43::6849 lt 7 lc "red" with lines      ti "Entladung nach 1C Ladung", \
     'Entladen0.5C.CSV' u (t=timecolumn(2,myFmt), $0==0?t02=t:0, t-t02):4 every ::43::6382 lt 7 lc "web-green" with lines ti "Entladung nach 0,5C Ladung", \
     'Entladen0.2C.CSV' u (t=timecolumn(2,myFmt), $0==0?t03=t:0, t-t03):4 every ::43::5928 lt 7 lc "blue" with lines      ti "Entladung nach 0,2C Ladung"

With this code I get the following plot:

Output from this code

It looks very good till now. But when I zoom in now, we can see, that there is an offset from 7 seconds on the green graph.

Zoom

Thats because I told my measuring-device to record, before the event happened. So I want the graph to begin with 7 seconds on x=0. But so that the time starts again from zero.

CodePudding user response:

You have to shift your data by the first time of the corresponding dataset. You can do this by storing the first time entry, i.e. when row index $0==0 into the variables t01,t02,t03 and subtracting these from all other values in the dataset.

The following is not using set xdata time but just plots the data as seconds but formatted on the x-axis as set format x "%tH:%M" timedate. Actually, %tH is not wrapping at 24 hours. Check help time_specifiers.

Edit:

  • take the milliseconds in column 3 into account. Define a function which adds 1/1000 of the value in column 3 to the time.
  • add a constant shift (here of 7 seconds) to one of the data sets.
  • use "%tM:%tS" for time formatting, especially if you want to display negative times.

Script:

### shift time by starting time
reset session

$Data1 <<EOD
2022-11-25 15:43:00;  0;   1.0
2022-11-25 15:43:17;200;   2.0
2022-11-25 15:44:00;400;   3.0
EOD

$Data2 <<EOD
2022-10-09 23:59:07;  0;   4.0
2022-10-09 23:59:37;200;   3.0
2022-10-10 00:00:07;400;   1.0
2022-10-10 00:00:37;800;   2.0
EOD

$Data3 <<EOD
2022-09-30 08:22:00;  0;   3.0
2022-09-30 08:22:14;400;   4.0
2022-09-30 08:23:11;800;   2.0
EOD

myFmt = "%Y-%m-%d %H:%M:%S"
set datafile separator ";"
set format x "%tM:%tS" timedate
set xlabel "Minutes:Seconds"
set yrange[0:5]

myTime(col1,col2) = timecolumn(col1,myFmt) column(col2)/1000
dt1 =  0
dt2 = -7
dt3 =  0

plot $Data1 u (t=myTime(1,2), $0==0?t01=t:0, t-t01 dt1):3 w lp pt 7 lc "red"       ti "Data1", \
     $Data2 u (t=myTime(1,2), $0==0?t02=t:0, t-t02 dt2):3 w lp pt 7 lc "web-green" ti "Data2", \
     $Data3 u (t=myTime(1,2), $0==0?t03=t:0, t-t03 dt3):3 w lp pt 7 lc "blue"      ti "Data3"
### end of script

Result:

enter image description here

  • Related