Gnuplot construct timefmt from different CSV columns? - linux

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

Related

Gnuplot using time as x-axis

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.

Plot data with quarterly tic marks

I am plotting data that is recorded on the last day of the 3rd, 6th, 9th, and 12th months of the year. I would like the tic marks to be at 03/09, 06/09, ...
After reading the documentation, I thought this could be done by saying
set xtics "03/09", 7889220
because there are about 7889220 seconds in three months. But rather than starting with March, 2009, the tic marks start on the next day, shown here (with the remaining part of the plot removed):
Is there a way to force the tic marks to be at end of months?
UPDATED:
The date format in the input file is mm/dd/yyyy, which I am reading with these commands:
set xdata time
set timefmt "%m/%d/%Y"
and I'm then doing this:
set format x "%m/%y"
set xrange ["03/31/2009":"12/31/2010"]
Discussion:
The Gnuplot behaviour when you use set format x "%m/%y" will be to place xtics at month boundaries since that really is what this command is asking Gnuplot to do.
To solve your problem there may be two posisble approaches that you can take here depending on how large your data set is.
Approach1:
If you do have time stamps in your data file one possibility is to just use the xtics directly for plotting (suitable if you have a large dataset)
So you do away with all the time commands in your script and just use
plot 'Mydata.dat' u 2:xtic(1) w points
Approach2:
The other option is to set custom xtics, however you will have to do this by hand and if you have a large dataset this might be cumbersome (suitable if the dataset has tens of points)
set xtics ("03/09" "03/31/2009", "06/09" "06/30/2009", "09/09" "09/30/2009", "12/09" "12/31/2009")
Will give you tics at the exact days you need them to be.
Assumption:
I assume that the first column in your file are the time stamps and second column are the data values. Below, I show a graph where I use the manual setting approach (Approach 2).
Result with dummy data:

gnuplot plot timeseries minute resolution in the x-axis

I have a time series at second level.
I'm using
set timefmt '%d/%m/%Y %H:%M:%S'
set xdata time
My x-axis have time with second precision (09:45:00)
is it possible to show time only at minute precision (09:45..09h50..etc) instead of(09:45:00..09:50:00 etc)
set timefmt sets the format which is used to read the data file. To set a different output format for the x-axis, use e.g.
set format x "%H:%M"
Then you must also set the xtics accordingly. To have a major tic every five minutes, use
set xtics 5*60

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.

Resources