Gnuplot date/time in x axis
Solution 1
Gnuplot doesn't actually expect time data to be in quotes, so you have to tell it:
set timefmt '"%Y-%m-%d %H:%M:%S"'
You can put the double quotes inside single quotes as I did here, or escape the quotes:
set timefmt "\"%Y-%m-%d %H:%M:%S\""
the same applies to your xrange
specification:
set xrange ['"2013-07-21 16:00"':'"2013-07-22 16:00"']
If you delete the quotes in the data file, then you can use the formatting you originally had, except the column numbers will be shifted over by 1 since the date takes up two columns without the quotes.
Solution 2
It seems like the answer is yes, the problem was the space.
doing this seems to fix it:
set datafile separator ","
and actually separating the times and data with commas.
Solution 3
As far as I understood, the order of instructions is important, I could afford it using:
- The
timefmt
is for data and has to be the same for xrange format x
is only for displayingset xdata time set timefmt "%Y%m%dT%H%M%S" set format x "%Y-%m-%d\n%H:%M:%S" # or with bash variables: set xrange ["$l_start_dt":"$l_end_dt"] set xrange ["20190620T000000":"20190628T000000"]
Jose Salvatierra
Hello, I'm Jose! I'm a software engineer turned online instructor. I help students learn about Python and web development by making online courses, writing blog posts, and answering questions here. My main topics of expertise are: Python Flask PostgreSQL MongoDB HTML, CSS, JavaScript React I'm currently learning more about Tkinter!
Updated on January 23, 2020Comments
-
Jose Salvatierra over 4 years
I have a quick question regarding dates and times in x-axis in GNUPLOT. I'll let the code do the talking:
This is my data:
#Time Data in Data out "2013-07-22 15:59:00" 6286 3730 "2013-07-22 15:58:00" 10695 14589 "2013-07-22 15:57:00" 17868 26464 "2013-07-22 15:56:00" 18880 34012 "2013-07-22 15:55:00" 19206 41192 "2013-07-22 15:54:00" 20365 43218 "2013-07-22 15:53:00" 18459 39298 "2013-07-22 15:52:00" 3420 4686 "2013-07-22 15:51:00" 3256 4942
And this is the code that is generating the graph:
gnuplot> set title "Data usage over the last 24 hours" gnuplot> unset multiplot gnuplot> set xdata time gnuplot> set style data lines gnuplot> set term png Terminal type set to 'png' Options are 'nocrop font "arial,12" fontscale 1.0 size 640,480 ' gnuplot> set timefmt "%Y-%m-%d %H:%M:%S" gnuplot> set format x "%m-%d\n%H:%M" gnuplot> set xlabel "Time" gnuplot> set ylabel "Traffic" gnuplot> set autoscale y gnuplot> set xrange ["2013-07-21 16:00":"2013-07-22 16:00"] gnuplot> set output "datausage.png" gnuplot> plot "C:\\Users\\blah\\Desktop\\plot.tmp" using 1:2 t "inbound" w lines, "C:\\Users\\blah\\Desktop\\plot.tmp" u 1:3 t "outbound" w lines ^ all points y value undefined!
Is the problem the space in between
date
andtime
in the x-axis? If not, what do you think could be the problem? -
andyras almost 11 yearsThis also works. It seems that when the space is the separator, the quotes are treated as part of the data, so you need to escape them. When the comma is the separator, gnuplot apparently ignores the quotes.
-
gaoithe over 8 yearsWorth emphasising the first line of this answer: <b>"Gnuplot doesn't actually expect time data to be in quotes, so you have to tell it:"</b>!! Thank you. Gnuplot actually seemed to parse the dates okay but got a really weird range with negative to positive years.
-
Ted almost 3 yearsAlso the using 1:2 is not optional. I got the error "Need full using spec for x time data" if I just used plot "/tmp/foo.gnuplot".