gnuplot multiple lines with Time on X axis - gnuplot

I've had a look through questions but still can't get this working.
My data set is like this:
[date] , [%cpu] , [mem]
23:00:39 , 21.9 , 2.1
23:00:44 , 21.8 , 2.1
23:00:49 , 21.8 , 2.1
23:00:54 , 21.8 , 2.1
23:00:59 , 21.7 , 2.1
My Gnuplot statements (just started using for this data) is:
set timefmt "%H:%m:%s"
set xdata time
set datafile sep ','
plot '/tmp/info' using 2 title 'cpu' with lines, '/tmp/info' using 3 title 'memory%' with lines
I get the following error:
Need full using spec for x time data
I've tried autoscale x , but I'm a bit lost, any help would be appreciated.

Time data requires that you always specify all columns to be used. (Note also the corrected timefmt):
set timefmt "%H:%M:%S"
set xdata time
set datafile sep ','
set style data lines
plot '/tmp/info' using 1:2 title 'cpu', '' using 1:3 title 'memory%'
The reason for this is, that the timefmt may also contain spaces, so that the data used for the time axis may come from two columns. Consider the following modified data file:
23:00:39 06/08/13 21.9 2.1
23:00:44 06/08/13 21.8 2.1
23:00:49 06/08/13 21.8 2.1
23:00:54 06/08/13 21.8 2.1
23:00:59 06/08/13 21.7 2.1
The plotting commands for this format are:
set timefmt "%H:%M:%S %d/%m/%Y"
set xdata time
set format x "%H:%M:%S"
set style data lines
plot 'mydata.dat' using 1:3 t 'cpu', '' using 1:4 t 'memory%'
To avoid confusion, it is always required that for time data all columns used by the plotting style (here with lines) are given explicitely with the using statement.

Your plot command look like this instead:
plot '/tmp/info' using 1:2 title 'cpu' with lines, '/tmp/info' using 1:3 title 'memory%' with lines

I had a similar issue with gnuplot where I had a data file like:
05:07:00 0.0769 0.0769 0.0000 0.0000 0.0000 0.0000 0.0000
05:08:00 0.2308 0.2308 0.0000 0.0000 0.0000 0.0000 0.0000
I was trying to use, but it was just not working
set xdata time
set timefmt "HH:MM:SS";
plot "wed" using 0:2 t 'wtf" w lines
The fix was a couple of things, primarily using the %'s in the timefmt string and only using one letter. Also using the set format x for the output was key (and using the first column as 1 - though I had tried that also earlier).
This is the minimal output script that is working for me:
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M"
plot "wed" using 1:2 t 'my title' w lines

Related

gnuplot stock chart using date on x axis

I am trying to use gnuplot for the first time.
I am completely new to gnuplot. Please forgive any basic mistakes.
I am trying to plot a stock chart.
My data looks like the following:
Date Open High Low Close
21/04/2017 31.81 32.09 31.67 31.95
20/04/2017 31.55 32.02 31.45 31.91
19/04/2017 31.3 31.71 30.99 31.57
18/04/2017 31.78 31.84 31.06 31.3
17/04/2017 31.3 31.97 31.21 31.8
13/04/2017 31.26 31.48 31.16 31.19
12/04/2017 31.13 31.38 30.98 31.24
11/04/2017 31.37 31.66 30.86 31.2
I am using the following settings to plot the lines. I got them from another website.
set xdata time
set timefmt "%d/%m/%Y"
set xrange ["21/04/2015":"21/04/2017"]
set format x "%d/%m/%Y"
plot [0:100] 'chart.dat' using 0:2:3:4:5 notitle with financebars
However, the x-axis just has 01/01/1970. See attached pic.
Any assistance would be greatly appreciated. Thanks.
The plot command plot [0:100] ... using 0:2:3:4:5 takes column 0 for x, column 0 corresponds to the line number instead of the time column.
This command should work:
plot 'chart.dat' using 1:2:3:4:5 notitle with financebars

gnuplot: Connect points even when missing/invalid are inbetween

I have a data file with interleaving data rows from different devices.
Now, I'd like to plot data from one devices with linepoints and use this to filter only the device of interest:
plot 'datafile' using (<someCondition> ? $1 : 1/0):2
Now, gnuplot does not connect the points because there is always some invalid data inbetween.
Is it possible to make gnuplot to connect my points?
By the way: This is a Windows machine, so an external sed/awk/whatever command is no option.
Since gnuplot version 5.0.6 you can use set datafile missing NaN to have invalid points treated as missing ones, and drawing with lines or with linespoints simply ignores those points and connects the others
$data <<EOD
12
27
0
23
42
EOD
set multiplot layout 1,2
set title '0.0 invalid'
plot $data using 0:($1 == 0.0 ? 1/0 : $1) with linespoints pt 7 notitle
set title '0.0 invalid but treated as missing'
set datafile missing NaN
replot
unset multiplot
Output with 5.0.6:

gnuplot additional parameter to X axis

I wonder how I can add a parameter to every X parameter. Like on the picture, where every X parameter has an additional parameter.
I run gnuplot with the following command
gnuplot -p -e "reset; set yrange [0:1]; set term png truecolor size 1024,1024; set grid ytics; set grid xtics; set key bottom right; set output 'Recall.png'; set key autotitle columnhead; plot for [i=2:3] 'Recall' using 1:i with linespoints linecolor i pt 0 ps 3
Recall file has the following content
train approach1 approach2
2 0.6 0.07
7 0.64 0.076
9 0.65 0.078
I wonder if I can add additional parameter as follows
train approach1 approach2
2(10) 0.6 0.07
7(15) 0.64 0.076
9(20) 0.65 0.078
The actual plotting should be according the real X parameters (2,7,9) an additional parameter is only for visualization and should be printed together with X.
Many gnuplot's terminals provide an enhanced option
that mimics the functionality provided by the postscript
terminal, functionality described here.
What you want can be done using an enhanced terminal in conjunction with the set xtics command (see help set xtics for the correct sintax):
gnuplot> set term qt enhanced
gnuplot> set xrange [2:10]
gnuplot> set xtics ('{/=8 3} {/=20 (a)}' 3, '6 (c)' 6)
gnuplot> plot sin(x)
Please refer to the link for a complete description of the available commands.
Update
To produce automatically the x axis labels, one can use backticks substitution, either directly in a gnuplot command file or on the command line, as in the OP approach.
The command line is longish...
gnuplot -p -e "reset; set yrange [0:1]; set term png truecolor size 1024,1024; set grid ; set key bottom right; set output 'Recall.png'; set key autotitle columnhead; `awk -f Recall.awk Recall` ; plot for [i=2:3] 'Recall' using 1:i with linespoints linecolor i pt 0 ps 3"
The key point is using an awk script that outputs the appropriate gnuplot command, and here it is the awk script
% cat Recall.awk
BEGIN { printf "set xtics (" }
NR>1 {
printf (NR==2?"":",")
printf ("'{/=8 %d} {/=16 (%d)}' %d", $1, $4, $1) }
END { print ")"}
Oooops!
I forgot to show the modified format of data file...
% cat Recall
train approach1 approach2
2 0.6 0.07 10
7 0.64 0.076 15
9 0.65 0.078 20
and here it is the product of the previous command line
If you want to take an xtic label from your data file, you can use using ...:xtic(1) which would take the value of the first column as xtic label.
The disadvantage might be, that for every value in your data file you'll get an xtic, and no other ones. So, using the data file
train approach1 approach2
2(10) 0.6 0.07
7(15) 0.64 0.076
9(20) 0.65 0.078
you could plot with
reset
set term png truecolor size 1024,1024
set grid ytics
set grid xtics
set key bottom right
set output 'Recall.png'
set key autotitle columnhead
plot for [i=2:3] 'Recall' using 1:i:xtic(1) with linespoints linecolor i pt 7 ps 3
and get
Note, that this uses the correct x-values only, because gnuplot itself drops the content inside the parenthesis, not being a valid number.
If you want to use different font sizes for the label parts, you could add an additional column which contains the parameter.
Data file Recall2
train add approach1 approach2
2 (10) 0.6 0.07
7 (15) 0.64 0.076
9 (20) 0.65 0.078
Now, instead of using xtic(1), you can also construct the string to be used as xticlabel:
reset
set term pngcairo truecolor enhance size 1024,1024
set grid ytics
set grid xtics
set key bottom right
set output 'Recall2.png'
set key autotitle columnhead
myxtic(a, b) = sprintf("{%s}{/*1.5 %s}", a, b)
plot for [i=3:4] 'Recall2' using 1:i:xtic(myxtic(strcol(1), strcol(2))) with linespoints linecolor i pt 7 ps 3

Gnuplot, yerrorbar, text as x axis

I have the following data:
t4.8k 1.84 1.86 1.83
t5.8k 1.82 1.84 1.8
t7.10k 1.79 1.8 1.77
t8.8k 1.8 1.84 1.76
I need to plot this in GNU plot using yerror bars.
Column1 - dataset name. This is the xaxis scale.
Column2 - Y-Mean
Column3 - Y-Max
Column4 - Y-Min
Here is the plotting code that I use:
plot "chameleonConfidence.dat" using xtic(1):2:4:3 title "Ratio of Time Taken" with yerrorbars
But this gives me the following error
Warning: empty x range [4.94066e-324:4.94066e-324], adjusting to [4.94066e-324:4.94066e-324]
"chameleonConfidence.gplot", line 15: x_min should not equal x_max!
Can someone help me with this?
This works for me.
set xrange [-1:4]
TITLE="Ratio of Time Taken"
plot "chameleonConfidence.dat" using ($0):2:4:3:xticlabels(1) with yerrorbars title TITLE
the xtic function doesn't replace the x-values in your file -- it is an automagic extra field you can add to the using specification to add the xticlabels on your axis. Basically, you just need the line number ($0) to be the x value and then you are all set (moving xtic(1) to the end of your using spec)

GNUPLOT Plotting 5 day financial week

I've been searching for a while now to find out how to remove days of the week from a financial plot with no success.
I need the plot to just include the days of the week and completely miss out the weekends such that there is no 2-day gap in the financial chart.
I have the data in CSV format Open/Low/Close/High and it has the weekend data missing, it plots fine but I can't find how to not show the weekends, any help would be really appreciated.
I'd like to see it say M/T/W/T/F/M/T/W/T/F on the X basically rather than M/T/W/T/F/S/S/M etc...
Cheers,
Chris.
As far as I know, this cannot be done with gnuplot itself - you need to bring the file into the desired shape before. If you are on Linux, this can be done with something like
awk '{if( index( $1, "S" ) == 0 ) print $0 >> "new.dat"}' old.dat
where old.dat is your original file and new.dat the new file without weekends. I have assumed here that your data file has the weekday as the first entry in each line.
This would work under Windows as well, but you would need to install Gawk for Windows first.
The data is not shown in the file, the file is just weekday based and misses the weekends. If you plot the data you get these 2day gaps at the weekend so I want to remove these gaps. It's more really to do with the x axis having weekends in to make it linear.
Here is an example of part of the file:
2006-03-23T16:59 1.7470 1.7324 1.7471 1.7344 0.0000 0.0000 0.0000 0.0000
2006-03-24T16:59 1.7346 1.7308 1.7441 1.7428 0.0000 0.0000 0.0000 0.0000
2006-03-27T17:59 1.7424 1.7415 1.7492 1.7459 0.0000 0.0000 0.0000 0.0000
2006-03-28T17:59 1.7462 1.7422 1.7537 1.7424 0.0000 0.0000 0.0000 0.0000
If you look at the dates, there are gaps in the file. There should be gaps because these days have no data. The graph however should run without gaps and that is what I am trying to achieve.
I just came across set xdtics today. I doubt you're still working on this, but maybe that will be helpful for someone else... (see help xdtics)
Assuming your data file isn't missing any weekdays, you can treat the date column as a string type. (If you're missing weekdays, your chart will skip over those dates without allocating any space for them, which is easy to miss, so beware.)
I have a date as the first column in my data file in YYYY-MM-DD format. The data I'm plotting is in the second column. Here are the relevant lines of my gnuplot configuration:
set format x '%s'
plot 'file' using 0:2:xtic(substr(strcol(1),6,10))
The set format line tells gnuplot how to print the x labels. The using config uses column 0 (the index) as the x parameter, column 2 (the data) as the y parameter, and provides special instructions for printing labels: only print characters 6–10. (This chops off the year portion, which helps the label fit without overlapping in my case.)
Also see this SO answer. I wouldn't want to replicate this "broken axis" solution for every weekend, but perhaps it could serve to inspire.
If you want to neglect the weekends on the time scale you can simply define a function which returns the day number after time(0) omitting the weekends. Note, that time(0) is 1970-01-01 00:00:00 for gnuplot 5.x and 2000-01-01 00:00:00 for gnuplot 4.x.
dw5(t) returns day number after time(0) omitting the weekends and NaN if t is a weekend day.
dw5tow7(n) returns the date from the "5-day-week" day number and NaN if input is NaN.
dw7Tic(n) returns the date label for xtic and empty string '' if input is NaN.
For example, the command:
do for [i=0:10] { print sprintf("%s % 4d",strftime("%Y-%m-%d",dw5tow7(i)),i) }
will return in gnuplot5.x:
1970-01-01 0
1970-01-02 1
1970-01-05 2
1970-01-06 3
1970-01-07 4
1970-01-08 5
1970-01-09 6
1970-01-12 7
1970-01-13 8
1970-01-14 9
1970-01-15 10
Script: (works for gnuplot>=5.0.0, Jan. 2015)
### remove weekends on time scale
reset
FILE = "SO9680677.dat"
# create some random test data
set print FILE
t0 = time(0)
y0 = 100
do for [i=0:60] {
t = t0 + i*3600*24
if (int(tm_wday(t)+1)%7>1) {
print sprintf("%s %g",strftime("%Y-%m-%d",t),y0=y0+rand(0)*1-0.5)
}
}
set print
SecPerDay = 3600*24
SecPerWeek = 7*SecPerDay
isWeekend(t) = int(tm_wday(t)+1)%7 < 2
myTimeFmt = "%Y\n%m-%d"
tOff = tm_year(0)==1970 ? 3 : 5 # offset gnuplot5.x: 3, gnuplot4.x: 5
dw5(t) = isWeekend(t) ? NaN : int(t/SecPerDay) - 2*int((t+tOff*SecPerDay)/SecPerWeek)
dw5tow7(n) = n==n ? n*SecPerDay + (int(n+tOff)/5)*2*SecPerDay : NaN
dw7Tic(n) = n==n ? strftime(myTimeFmt,dw5tow7(n)) : ''
set key top center out noautotitle
set grid x,y
set ytics 1
set multiplot layout 2,1
set xrange[:] noextend
set format x myTimeFmt timedate
plot FILE u (timecolumn(1,"%Y-%m-%d")):2 w lp pt 7 lc rgb "red" ti "with weekends"
set format x "%g\n" numeric
plot FILE u (dw5(timecolumn(1,"%Y-%m-%d"))):2 w lp pt 7 lc rgb "web-green" ti "without weekends", \
'' u (t0=dw5(timecolumn(1,"%Y-%m-%d"))):(NaN):xtic(dw7Tic(t0)) every 5
unset multiplot
### end of script
Replace the above multiplot section with the snippet below and the script will run with gnuplot>=4.6.0 (March 2012). Maybe with further tweaking it can be made work with gnuplot 4.4.0.
### version for gnuplot 4.6.0, March 2012
set multiplot layout 2,1
set timefmt "%Y-%m-%d"
set xdata time
set format x myTimeFmt
plot FILE u 1:2 w lp pt 7 lc rgb "red" ti "with weekeends"
set format x "%g\n"
plot FILE u (dw5(timecolumn(1))):2 w lp pt 7 lc rgb "web-green" ti "without weekends", \
'' u (t0=dw5(timecolumn(1))):(NaN):xtic(dw7Tic(t0)) every 5 w p
unset multiplot
Result:
Actually, after all, the larger your time range the less you will notice if there are weekends or not.
Using some external tool (I would wrote a bash or python script for that, I believe; it should not be difficult), you can insert lines for weekend days (one line for a day) into your data file, like this:
2006-03-26T00:00 NaN NaN NaN NaN NaN NaN NaN NaN
(or you can just append those NaNs for weekends at the end of data file and use unique keyword)
and then plot, let's say, the first data with using 1:($2) with linespoints, not using 1:2 ...
This should work for you.

Resources