Search code examples
gnuplotxrange

Gnuplot: how to set xrange with set xtics time?


I plot a time-series of datas (images, throughput), using this:

set term postscript color eps enhanced 22
set encoding utf8
set output "tput.eps"
load "../styles.inc"
set size 1,0.6
set bmargin 4.5
set tmargin 2.5
set lmargin 9
set rmargin 8
set title "{/bold Images updated and generated network traffic}" offset 0,0
set ylabel "#Images" offset 0,0
set y2label "Throughput [GB/day]" offset -2,0
set ytics 0,100,800
set ytics nomirror

set y2tics 0,100,600
set y2tics nomirror

set xtics time
set format x '%d/%m/%Y'

set grid y
set yrange[0:800]
set y2range [0:]
set xtics rotate by 45 right font "Arial, 18" nomirror
set key vertical sample 1.0 maxrows 1 width -0.6 at graph 1,1.13 font "Arial, 18"

set datafile separator ","
plot "datecount_sorted.csv" using (timecolumn(1, "%Y-%m-%d")):($2) with lines ls 5001 title "Uploaded images",\
     "datecount_sorted.csv" using (timecolumn(1, "%Y-%m-%d")):($3/(1024*1024*1024)) axis x1y2 ls 5002 title "Upload Throughput",\
                     
!epstopdf "tput.eps"
!rm "tput.eps"
quit 

The input data looks like this:

2016-04-12,1,0
2016-05-02,2,0
2016-05-05,2,0
2016-05-06,1,0
2016-05-11,2,0
2016-05-13,3,0
2016-05-25,2,0
2016-06-01,3,541204241
2016-06-06,1,0
2016-06-13,1,471311979
2016-06-14,1,6329289
2016-06-17,1,137972881
2016-06-24,1,319050239
2016-06-27,1,138193384

The output is correct, it looks like this: enter image description here

The problem is that when I try to set an xrange, the plot breaks completely (does not render). This syntax in particular seems to be wrong:

set xrange ["2020-01-01":"2020-06-17"]

And similarly wrong is the one using the same timefmt that is used in the plot:

set xrange ["01/01/2020":"17/06/2020"]

So..what is the correct syntax ?


Solution

  • What you are missing is set xdata time and set timefmt <format string> to tell gnuplot how read the dates from the data, the xrange is set using the format specified in timefmt, which doesn't have to be the same as the format you give to the axis, the plot breaks because it can't decode the dates from the xrange. In your case adding this should suffice

    set xdata time
    set timefmt "%Y/%m/%d"
    

    I tested it with set xrange ["2016/04/12":"2016/05/03"] on the example data file you posted.