Gnuplot using time as x-axis - gnuplot

I am trying to use gnuplot from a csv file. There are quite a bit of similar posts but I haven't had any success from them. There should be 4 lines marked by the integer values and the times should make up the x axis.
data file: first column is H:M:S, rest are 0-100 values
00:26:45,34,12,3,7
00:27:14,31,10,5,9
00:27:43,27,7,2,4
00:28:18,32,11,4,6
00:28:45,36,17,6,7
00:29:35,39,16,1,3
00:30:48,24,12,5,4
00:31:33,32,9,2,8
00:32:15,27,12,3,7
00:32:45,34,15,6,4
gnuplot commands:
set datafile separator ","
set yrange [0:100]
set xrange ["00:00:00":"24:00:00"]
set xtics format '%H:%M:%S'
set timefmt '%H:%M:%S'
set xdata time
plot 'test.csv' using 1:2 with lines
So far all I get is a blank plot with 5 second increments starting at 00:00:00.
Any help would be greatly appreciated.

I was able to get the plot to show correctly by removing the xrange line. But the x-axis still doesnt display the exact times from the first column, Just every hour. Seems to just be a matter of more formatting.

Related

Gnuplot misreads time data

I can't seem to get gnuplot to properly plot my time data. (I'm using version 4.6, patchlevel 3.)
For an MWE (well, non-working...), the input file
reset
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M:%S"
plot '-' using ($1):($2) with lines lw 3
15:36:12 1.0
15:43:17 3.0
16:12:02 2.0
e
produces the following output:
Apparently, gnuplot interprets the hours as seconds and ignores the rest. I have tried a bunch of other format strings (also single/double quotes), but gnuplot seems to ignore everything except the first number.
Is something wrong with my format, or is this a bug, or something else?
With $1 you explicitely select the numerical value of the first column, which bypasses any gnuplot automatism to interpret the column values as appropriate (as time value in your case). Simply use using 1:2:
reset
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M:%S"
plot '-' using 1:2 with lines lw 3
15:36:12 1.0
15:43:17 3.0
16:12:02 2.0
e
Use the syntax $1 only when doing calculations with the actual numerical value in the respective column. $1 is a shortcut for column(1), gnuplot also knows stringcolumn(1) and timecolumn(1) for other purposes.

Gnuplot construct timefmt from different CSV columns?

I have the following csv file in which the columns represent hour,minute,view count respectively, exactly 1440 entries for every minute of the day. I would like to plot it using gnuplot. (Putting the HH:MM into one column is not possible due to the MySQL ONLY FULL GROUP BY.)
"0","0","71"
"0","1","55"
"0","2","56"
...
"23","57","66"
"23","58","70"
"23","59","75"
Currently I use the following commands:
set datafile separator ","
set timefmt "%H,%M"
set xdata time
set xtics format "%H:%M" time font "/:normal,8" rotate by 45 offset -1,-1
The time format is not rendered correctly, the hours are interpreted as minutes and the minutes are not shown at all.
I also tried set timefmt "\"%H\",\"%M\"" without success.
Two questions:
How can I parse the hour:minute and display it correctly?
If this data file had more columns and the timestamp was not in the first but in the n-th one, then how can I specify for gnuplot to render that, similar to using 0:n for plot?
Use this time format string
set timefmt "%H:%M"
Put this function somewhere:
catdate(x,y)=sprintf("%d:%d",x,y)
Then plot with:
plot 'data.dat' u (catdate($1,$2)):($3) w l

gnuplot: xdata time and boxes

I have some temperature data in a text file, and I would like to represent it in a 'boxed' plot, showing the temperature of each day like an histogram.
23 10 2012 12.3
28 10 2012 14.1
30 11 2012 30.4
...
I'm triying to represent it with a simiple gnuplot script like this:
set terminal png enhanced font font_file size size_x, size_y tiny
set xdata time
set timefmt "%d %m %Y"
set format x "%d"
set boxwidth 0.9 relative
plot u 1:4 w boxes
I would like to leave blank the days where no data is available, but gnuplot gives these days the value of the last day a data was available. For example, in the data file I wrote before, gnuplot would give a 12.3 to the 23th of october, but I would like leave this gap without any bar.
Is there a way to get this? I have discarted an histogram representation because I've read that it is not compatible with time data.
Thank you in advance
Your problem is the line set boxwidth 0.9 relative. Relative says that you're trying to fill 90% of the space between adjacent boxes. You probably want to set an absolute boxwidth. If you change your script to set boxwidth 0.9 absolute, then you'll see vertical lines. This is because when using time data, the x-axis unit is actually seconds, so your box is only ~1 second wide when your x-scale is multiple days. So, to get each box to be the width of a single day you would use
set boxwidth 3600*24
Here's the complete script:
set term png enhanced
set output 'foo.png'
set xdata time
set timefmt "%d %m %Y"
set format x "%d"
set boxwidth 3600*24
plot 'test.dat' u 1:4 w boxes
and the output:

Month tics, how to set

In GnuPlot:
How to set xtics to 1st of every month?
set xtics would not work for me the as the number of seconds per month varies.
set xmtics does not work for me because months are displayed without years and it is not shown to which year belongs a month.
It seems to me that gnuplot handles time surprisingly well. I assume your data in the following (or similar) format:
05/17/12 0.8188064359
05/18/12 0.97297057
05/21/12 0.9012782504
I used random numbers spread over year and a half to test this.
So first you need to tell gnuplot your x coordinate is time and what is the format it is written in:
set xdata time
set timefmt "%m/%d/%y"
From now on, gnuplot is expecting all ranges in the specified format, including the xtics and xrange commands. The only exception is the increment in xtics which should be in seconds.
set xrange ["05/01/12":"08/01/13"]
set xtics "01/01/12",2592000,"08/01/18"
Now you might argue, that the number of seconds in each month is different by a whole day (actually 2 days in February). However, gnuplot authors seem to think about this as well and they solved the problem to our liking. In other words, the above will ensure tics every 1st of a month. Here, I can only suggest to specify the printing format of the xtics
set format x "%d %b %Y"
which will result in "01 May 2012", etc.
After this, plot your data. Oh, when using time on x-axis, gnuplot requires using in the plot command, so even with a trivial data file as I had, I had to use
plot 'data.txt' using 1:2
I used gnuplot version 4.6 patchlevel 0 and output to postscript terminal.
If you will have different experience with your version or terminal, let me know in the comments.
set xtics will work ok if you set the number of seconds to the average number of seconds in a month.
I worked this out by assuming 365.2425 days in the average year, dividing by 12 to find the average days per month (approximately 30.4), multiplying by hours per day then seconds per hour, i.e:
365.2425 / 12 * 24 * 3600 = 2629746
Assuming your data was formatted as year-month and value, i.e:
2016-01 10000
2016-02 12000
2016-03 10500
Then you’d need commands such as:
set xdata time
set timefmt "%Y-%m"
set format x "%b/%y" # Or some other output format you prefer
set xtics "2016-01", 2629746, "2016-03"
plot "mydata.dat" using 1:2 with linespoints
If you want to set xtics in multiples of months then just add a multiplier in the set xtics line, e.g to get quarterly marks:
set xtics "2016-01", 3*2629746, "2016-12"
I found that if I assumed the number of days in a month to be 30 or 31 then some at some point in the chart, months would be missing their xtics.

how to overlay 2 graphs in a single plot in GNUplot

I have two files that has the time as x axis and a value. I need to overlay these two on a single plot. Currently i tried it using GNUplot, but struck in the middle. Here is a sample file
01:03:05 6
01:03:15 6
and another file
01:03:55 6
01:04:10 6
I need to plot these two files (say x mark and some other symbol for differentiation) in a single plot. I dont know if it is possible to do that in GNUplot. Currently I have created two grids for each file. But I need both in a single plot. Here is what I have written
set multiplot layout 1,2 # engage multiplot mode
set xdata time ## these three lines control how gnuplot
set timefmt '%H:%M:%S' ## reads and writes time-formatted data.
set format x '%H:%M:%S' ##
set xtics 05 # make time spacing of 2 minutes
plot 'AAA' u 1:2 # plot the first data set
plot 'BBB' u 1:2 # plot the second data set
unset multiplot
Can anyone familiar with GNUplot or any other tool (working in linux) can help me.
In order to plot multiple lines in a single plot, simply put them in a single plot command like
plot 'AAA' u 1:2, 'BBB' u 1:2
There are numerous examples out there that give you a good start with gnuplot. This one for example shows how to plot multiple lines in one plot.
The multiplot command you are using in your script would also make it possible to have multiple plot windows like shown here. You can adjust the position of each subplot by:
set size XSIZE,YSIZE #see `help set size`
set origin XORIGIN,YORIGIN #see `help set origin`
or (if you have gnuplot 4.2 or newer):
set lmargin at screen XMIN #see `help margin`
set rmargin at screen XMAX
set tmargin at screen YMAX
set bmargin at screen YMIN

Resources