Gnuplot timefmt with different lengths - csv

I try to plot sth out of two .csv files.
In the first there is the timeformat %Y-%m-%d %H:%M:%S and in the second %H:%M:%S.
It isn't possible for me, to show both graphs at once.
When i erase the "set timefmt/xrange "%Y-%m-%d %H:%M:%S"", only the other graph is showed and the other way around.
Somebody have an idea, what I can do?
File1:
1;2022-11-24 17:21:34;0;+3.311;+0.004;+0.003;+0.001;+0.000;+0.000;+0.000;+0.000;+0.001;-0.001;+0.001;+0.000;-0.001;+0.000;-0.001;+0.000;+0.000;-0.001;+0.002;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
2;2022-11-24 17:21:34;200;+3.311;+0.007;+0.002;+0.001;-0.001;+0.000;+0.000;-0.001;+0.001;-0.001;+0.001;+0.000;-0.002;+0.001;-0.001;+0.000;+0.001;-0.001;+0.001;-0.001;LLLLLLLLLL;LLLLLLLLLL;LLLLLLLLL
...
File2:
17:22:28;3.446;1.398;0.007;4.817508;0.025
17:22:29;3.447;1.398;0.008;4.818906;0.027
17:22:30;3.448;1.398;0.008;4.820303999999999;0.029
...
My code:
set grid
set datafile separator ";"
set title 'xxx'
set title font ",12"
set ylabel 'U/V' font ",12"
#set format x "%H:%M:%S"
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 [3:3.7]
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 "%Y-%m-%d %H:%M:%S"
set timefmt "%H:%M:%S"
#set xrange ["2022-11-24 17:22:00":"2022-11-24 18:47:00"]
set xrange ["17:20:00":"18:47:00"]**
plot'xxx.CSV' using (timecolumn(2, "%Y-%m-%d %H:%M:%S")):4 every ::43::25741 title "aaa" lt 7 lc 7 with lines, \
'yyy.csv' using (timecolumn(1, "%H:%M:%S")):2 title "bbb" lt 3 lc 6 with lines
My code:
set grid
set datafile separator ";"
set title 'xxx'
set title font ",12"
set ylabel 'U/V' font ",12"
#set format x "%H:%M:%S"
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 [3:3.7]
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 "%Y-%m-%d %H:%M:%S"
set timefmt "%H:%M:%S"
#set xrange ["2022-11-24 17:22:00":"2022-11-24 18:47:00"]
set xrange ["17:20:00":"18:47:00"]**
plot'xxx.CSV' using (timecolumn(2, "%Y-%m-%d %H:%M:%S")):4 every ::43::25741 title "aaa" lt 7 lc 7 with lines, \
'yyy.csv' using (timecolumn(1, "%H:%M:%S")):2 title "bbb" lt 3 lc 6 with lines

I think the problem is that only one of your data files gives a specific date.
If you read in time data using format "%H:%M:%S" (no year/date given) then the times are assumed to be relative to the epoch date 1-Jan-1970. So those data points come out 52 years off from the 2022 data points.
Option 1:
If all the data points in both files are from the same day, then I suggest the easiest thing to do is skip the date information in the file where it is present. I.e.
set timefmt "%H:%M:%S"
set xrange ["17:20:00":"18:47:00"]
plot 'xxx.csv' using (timecolumn(2, "2022-11-24 %H:%M:%S")):4 title "aaa" \
'yyy.csv' using (timecolumn(1, "%H:%M:%S")):2 title "bbb"
The string "2022-11-24" must match on input for the first file but it doesn't actually contribute to the date calculation.
Option 2:
If you really do care about the date, or if the first file spans multiple dates so that a constant string cannot match, then you could instead add a date component to the time string in the second file by concatenating a string constant containing the date.
myfmt = "%Y-%m-%d %H:%M:%S"
set timefmt "%Y-%m-%d %H:%M:%S"
set xrange ["2022-11-24 17:20:00":"2022-11-24 18:47:00"]
plot 'xxx.csv' using (timecolumn(2, myfmt)):4 title "aaa" lt 7 lc 7 with lp, \
'yyy.csv' using (strptime(myfmt,"2022-11-24 ".strcol(1))):2 title "bbb" lt 3 lc 6 with lp

Related

How can I make a bar graph displaying y-min and y-max data along with custom colors for each bar?

I would like to make a bar graph that displays each bar as the bottom of the bar being y-min and the top of the bar being y-max. In addition I'd like each bar to have its own color fill and custom tics where xtic(1). Thank you
Similar to this below:
How to make range bar graphs in gnuplot?
ex.
#number #y-min #y-max #rgb
1 2 5 1
2 2.5 5.4 2
3 4 6 3
4 4.3 7 4
Here is what I ended up doing in case someone else needs it
set termoption font ",22"
boxwidth = 0.5
set style fill solid
set xtics nomirror
set ytics nomirror
set xtics font "{/:Bold,22}"
set ytics font "{/:Bold,22}"
set xlabel "Structure"
set xlabel font "{/:Bold,30}"
set ylabel "Potential (V vs. RHE)"
set ylabel font "{/:Bold,30}"
set xrange [-1:4.5]
set yrange [0:7]
set arrow from -1,2.5 to 4.5,2.5 nohead lc rgb 'black' dt 3
plot 'poten-range-plot' u 0:(($2+$3)/2.):(boxwidth/2.):(($3-$2)/2.):4:xtic(1) w boxxyerrorbars notitle lw 3 lc variable, 1/0 t "Pt (111)" lt 1 lc rgb 'black'

Use column from CSV as a category label for plotting column chart using gnuplot

I have a CSV file looking like:
frameNo dataSeg paritySeg frameType
0 17 3 k
1 2 1 d
2 3 1 d
3 3 1 d
4 3 1 d
5 2 1 d
6 3 1 d
7 3 1 d
8 4 1 d
I'm able to plot stacked column diagram showing number of data and parity segments per frame. Looks like this:
What I'd like to add to it, however, is paint differently those columns (both data and parity) which have "k" marker in the last column. Basically, distinguish between two categories - "d" and "k".
Is that possible using gnuplot?
Here's the script I'm using:
set style histogram rowstacked;
set style data histograms;
set style fill solid;
set datafile separator "\t";
set terminal png size 2500,1500 enhanced font ",30";
set title "";
set tics font ",25";
set xlabel "Frame #" font ",25";
set ylabel "# of segments" font ",25";
set key outside;
set xrange [0:];
plot "segments.csv" using 2 t "Data", "" using 3 t "Parity";'
You could impose a custom condition on the columns being plotted and supply an invalid value (signaling to skip the particular data point) if this condition is not met:
set terminal pngcairo size 1200,600 enhanced font ",30";
set output 'test.png'
set style histogram rowstacked;
set style data histograms;
set style fill solid;
#set datafile separator "\t";
set title "";
set tics font ",25";
set xlabel "Frame #" font ",25";
set ylabel "# of segments" font ",25";
set key outside;
set xrange [0:];
fName = 'segments.csv'
plot \
fName using (strcol(4) eq 'd'?$2:1/0) t "Data d" lc rgb '#666666', \
fName using (strcol(4) eq 'd'?$3:1/0) t "Parity d" lc rgb '#ff0000', \
fName using (strcol(4) eq 'k'?$2:1/0) t "Data k" lc rgb '#000000', \
fName using (strcol(4) eq 'k'?$3:1/0) t "Parity k" lc rgb '#990000'
this would give (using the sample data in your question):

GNUPlot if statement on plot

I've a csv data file like this:
Sensor1;value;iteration
Sensor2;value;iteration
Sensor2;value;iteration
Sensor1;value;iteration
Sensor2;value;iteration
Can I plot two different lines in base of my 1st col value? one for Sensor1 and another for Sensor2 in same plot.
Now I plot all data as follow:
set terminal jpeg
set output 'testimage.jpeg'
set autoscale # scale axes automatically
unset log # remove any log-scaling
unset label # remove any previous labels
set xtic auto # set xtics automatically
set ytic auto # set ytics automatically
set datafile separator ";"
set xrange [1:10000]
set yrange [3000:5000]
plot "result_test_day_1.csv" using 5:3:(stringcolumn(1) eq "Sensor1"? $2:1/0) title "a" lc rgb "blue" with lines
plot "result_test_day_1.csv" using 5:3:(stringcolumn(1) eq "Sensor2"? $2:1/0) title "b" lc rgb "red" with lines

Gnuplot stats does not work as expected: max value not right

Assuming to have the following 4 datasets:
a.csv
1,1
2,3
3,5
5,6
6,9
7,9
8,10
9,12
10,13
b.csv
1,1
2,5
3,10
5,15
6,20
7,25
8,30
9,35
10,40
c.csv
1,1
2,10
3,100
5,1000
6,2000
7,5000
8,10000
9,20000
10,50000
d.csv
1,1
2,20
3,300
5,5000
6,9000
7,10000
8,15000
9,30000
10,100000
In Gnuplot I've tried to run the command stats on each of them to get the maximum value for x and y (i.e., columns 1 and 2) and to set the corresponding xrange & yrange. Unfortunately, the result is not the one I've expected.
Here is the full script:
#!/usr/bin/env gnuplot
set terminal latex
set term pngcairo enhanced size 1500,800
set output 'plot.png'
set multiplot layout 2,2
set xlabel 't' font ',16'
set ylabel '#pkt' font ',16'
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set ytics font ',14'
set key font ',12'
set title font ',20'
set datafile separator ','
###
set title '(a)'
stats "a.csv" using 1:2 name "a"
set xrange [0:a_max_x]
set yrange [0:a_max_y+a_max_y*0.5]
plot "a.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(b)'
stats "b.csv" using 1:2 name "b"
set xrange [0:b_max_x]
set yrange [0:b_max_y+b_max_y*0.5]
plot "b.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(c)'
stats "c.csv" using 1:2 name "c"
set xrange [0:c_max_x]
set yrange [0:c_max_y+c_max_y*0.5]
plot "c.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(d)'
stats "d.csv" using 1:2 name "d"
set xrange [0:d_max_x]
set yrange [0:d_max_y+d_max_y*0.5]
plot "d.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
unset multiplot
and the result:
As you can see, maximum values in the plots b, c and d are not correct. Indeed, the verbose output of stats returns:
[...]
Maximum: 10.0000 [8] 13.0000 [8]
[...]
Maximum: 5.0000 [3] 15.0000 [3]
[...]
Maximum: 2.0000 [1] 10.0000 [1]
[...]
Maximum: 1.0000 [0] 1.0000 [0]
[...]
Apparently, only stats for the plot a is right. Is there anything wrong in my script?
You need you reinitialize xrange and yrange after setting them each time, because otherwise stats finds some of you points outside the range you have previously set and does not take them into account. It's the last line below:
set title '(a)'
stats "a.csv" using 1:2 name "a"
set xrange [0:a_max_x]
set yrange [0:a_max_y+a_max_y*0.5]
plot "a.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
set xrange [*:*] ; set yrange [*:*] # <--- This line after each plot will fix your issue
In your case there is no need to use stats in order to set the ranges.
Your requirements are:
Use tight limits for the xrange and the yrange. You get this with set autoscale fix.
Extend the maximum of the yrange by 50%. That is achieved with set offsets 0,0,graph 0.5,0:
#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,800
set output 'plot.png'
set multiplot layout 2,2
set xlabel 't' font ',16'
set ylabel '#pkt' font ',16'
set grid xtics ytics lt 0 lw 1 lc rgb "#333333"
set tics font ',14'
set key font ',12'
set title font ',20'
set datafile separator ','
set style data lines
set style line 1 linewidth 3 linecolor rgb 'blue'
###
set title '(a)'
set autoscale fix
set offset 0,0,graph 0.5,0
plot "a.csv" using 1:2 title 'v1' linestyle 1
###
set title '(b)'
plot "b.csv" using 1:2 title 'v1' linestyle 1
###
set title '(c)'
plot "c.csv" using 1:2 title 'v1' linestyle 1
###
set title '(d)'
plot "d.csv" using 1:2 title 'v1' linestyle 1
###
unset multiplot
One further comment: If you're going to use a LaTeX-based terminal for your actual image, don't use latex, but rather epslatex, cairolatex, context or lua tikz, which are all much better regarding the supported features and quality.

gnuplot ignores divisor of csv-column values

I am using gnuplot to draw a histogram of a series of RAM measurements I performed.
However, I want it to display the values that are stored in Bytes in CSV files in KB.
I divided the respective columns by 1024, but gnuplot simply ignores that.
Below you see the template that is changed by a script to have meaningful file names for CSVFILE and PSFILE and then fed into gnuplot.
set style data histogram
set style histogram errorbars gap 1
set xlabel "nodes"
set ylabel "memory (KB)"
set key left box
set datafile separator ","
set terminal postscript landscape
set output 'PSFILE'
plot 'CSVFILE' using ($2/1024):($3/1024):xtic(1) ti col lt -1 fs pattern 1,\
'' using ($4/1024):($5/1024):xtic(1) ti col lt -1 fs pattern 2,\
'' using ($6/1024):($7/1024):xtic(1) ti col lt -1 fs pattern 4,\
'' using ($8/1024):($9/1024):xtic(1) ti col lt -1 fs pattern 6,\
'' using ($10/1024):($11/1024):xtic(1) ti col lt -1 fs pattern 5,\
'' using ($12/1024):($13/1024):xtic(1) ti col lt -1 fs pattern 7,\
'' using ($14/1024):($15/1024):xtic(1) ti col lt -1 fs pattern 3
So what does not work is the /1024. Any ideas how to do that?
Changing the CSV files instead came to my mind, yes, but they are a lot, and I would have to write a script to change all cells, which I definitely do not fancy to do.
Okay, the solution was trivial. I just had to enclose the $2 values in extra braces, like ($2)/1024.