#171 Unequally spaced tics on time axis

None
accepted
nobody
None
5
2014-07-07
2014-07-04
Jorge Stolfi
No

When using time data on the X axis, the major tics are rounded to start-of-month dates, even if the tics interval was specified as some round number of days. How do I force the tics to be placed at the specified interval?

Test file:

set term png size 800,200 font "arial,10"
set output "bug-timetics.png"
set timefmt "%Y-%m-%d"
set xdata time
set format x "%Y-%m-%d"
nweeks=6 # Spacing of major tics in weeks
set xtics "2011-12-18",nweeks7246060,"2014-12-31"
set mxtics nweeks

plot "-" using (timecolumn(1)):2 title "dots" with linespoints pt 7 ps 2
2012-07-25 10.00
2012-08-30 15.00
2013-01-02 8.00
e
quit

Note that the major tics should be exactly 6 weeks apart, but are being placed instead at (some) start-of-month days. The minor tics following each major tic are 7 days apart, as they they should, but start againg at every major tic and etend beyond the next major tic, so that they end up irregularly spaced too.

Discussion

  • Jorge Stolfi
    Jorge Stolfi
    2014-07-04

    PS. I am using gnuplot 4.6 patchlevel 1

     
  • Jorge Stolfi
    Jorge Stolfi
    2014-07-04

    PS. the increment in "set xtics" should be "nweeks [asterisk] 7 [asterisk] 24 [asterisk] 60 [asterisk] 60". The asterisks got eaten as formatting codes.

     
  • Ethan Merritt
    Ethan Merritt
    2014-07-04

    I think the best approach is to not tell gnuplot that x is a time axis. That means you have to convert x-coordinat-in-seconds to a date yourself, but that's not so hard. This is easier in version 5, but so far as I know it is already possible in version 4.6.

    So you want something like:

    #
    # Comment these out - we will do the xtic formatting manually
    # set xdata time
    # set format x "%Y-%m-%d"
    #
      nweeks = 6
      timefmt = "whatever"
      starttime = strptime( ... )
      endtime = strptime( ... )
      time = starttime
      do while (time < endtime) {
          set xtic add (strftime(timefmt, time) time)
          time = time + nweeks * 7 * 24 * 60 * 60
      }
    

    (moving this to Support Requests rather than Bugs)

     
  • Ethan Merritt
    Ethan Merritt
    2014-07-04

    Ticket moved from /p/gnuplot/bugs/1438/

     
  • Jorge Stolfi
    Jorge Stolfi
    2014-07-06

    Thanks!

     
  • Jorge Stolfi
    Jorge Stolfi
    2014-07-06

    I had to fiddle a bit and it ALMOST worked, but I wasn't able to suppress the automatic tics. It seems to ignore the "unset xtics", and "set xtics ( )" does not work either.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    #! /usr/bin/gnuplot
    set term png size 800,300 linewidth 2 font "arial,8"
    set output "bug-timetics-fix.png"
    datefmt = "%Y-%m-%d"   # Format of times.
    set timefmt "%Y-%m-%d" # Format of times in data file.
    stmaj = 4 # Spacing of major tics in weeks
    stmin = 1 # Spacing of minor tics in weeks
    inisecs = strptime(datefmt, "2011-01-01")
    finsecs = strptime(datefmt, "2011-03-31")
    set xrange [(inisecs):(finsecs)]
    unset mxtics
    unset xtics
    set format x ""
    k = 0
    xtime = inisecs
    while (xtime <= finsecs) {
        minor = ((k % stmaj) != 0)
        xlab = strftime(datefmt, xtime)
        set xtics add scale 3,2 ( xlab xtime minor )
        k = k + stmin
        xtime = xtime + stmin * 7 * 24 * 60 * 60
    }
    show xtics
    set yrange [0:20]
    
    plot "-" using (timecolumn(1)):2 title "dots" with linespoints pt 7 ps 2
    2011-01-15 10.00
    2011-01-22  8.00
    2011-01-29 10.00
    2011-02-05 12.00
    2011-02-12 15.00
    2011-02-19  5.00
    2011-02-26  7.00
    e
    quit
    
     
  • Ethan Merritt
    Ethan Merritt
    2014-07-06

    Yeah, the "set xtics add" in the loop does not erase the pre-existing xtics setting. So at the top of the script use "set xtics" to place a single tic somewhere off-screen. E.g. "set xtics (-999)"

     
  • Jorge Stolfi
    Jorge Stolfi
    2014-07-06

    Thanks! What about "unset xtics" and "set xtics ( )" not working; should I report them as bugs?

     
  • Ethan Merritt
    Ethan Merritt
    2014-07-06

    In what way are they not working?
    Is it that you expect the sequence "unset xtics; set xtics add (<foo>)"
    to be different from "set xtics; set xtics add (<foo>)"?

    As to "set xtics ()", I am surprised that doesn't get flagged as an error.

     
  • Jorge Stolfi
    Jorge Stolfi
    2014-07-06

    I expected "unset xtics" to discard previously defined tics and suppress the automatic tics, so that "set xtics add ( ... )" would begin with an empty set of tics.

    I expected "set xtics ()" to have the same effect as "unset xtics", namely discard previously set tics, turn off the automatic tics, and start with an empty set of tics.

    I would expect "set xtics", with no other args, to turn on the default tics, which would be preserved by "set xtics add ( ... )" (and result in a mess).

    While not explicitly allowed by the documentation, people would expect the notation "()", in that context, to be understood as an empty list of explicit labels.

     
  • Ethan Merritt
    Ethan Merritt
    2014-07-07

    While not explicitly allowed by the documentation, people would expect the notation "()", in that context, to be understood as an empty list of explicit labels.

    Sounds reasonable. We can add that.

     
  • Ethan Merritt
    Ethan Merritt
    2014-07-07

    • status: open --> accepted
    • Group: 5.0 -->